Find the answer to your Linux question:
Results 1 to 6 of 6
Hi, I have written a basic shell script in Korn that quite happily works, but there are 21 reports to be checked, and they must be done individually. However, my ...
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Location
    Surrey, UK
    Posts
    5

    Arrays with multiple commands in Korn Shell

    Hi,

    I have written a basic shell script in Korn that quite happily works, but there are 21 reports to be checked, and they must be done individually. However, my boss has suggested using an Array, to trim down the script and make our Operations guys lives easier. I've never used Array's before and all I can see is setting pre-determined values for each Array entry. The script checks to ensure the existence of particular reports that we have. They check for the files existence. That it is not a zero size file, and that it is for today's date. (Ops would run this script at about 2am.)

    My question is how can I setup an Array to run the below once only for all 21 reports to be checked so as to create one load of output to the screen instead of having to run it individually 21 times ??

    Im really stuck on this and can't work it out. (Probably just too close to the problem....but pregnant wife, lack of sleep etc etc probably doesn't help lol)
    Any help would be greatly appreciated. Im sure its easy once I get past the mental block, I just can't seem to get my head round it.

    Choose an option:
    EOF

    read ans
    case $ans in
    1)clear
    test -f REPORT1.exp.log.Z
    if true
    then echo "Report 1 exists"
    else echo "Report 1 does not exist"
    fi

    test -s REPORT1.exp.log.Z
    if true
    then echo "Report 1 is valid file"
    else echo "Report 1 is 0 size file"
    fi

    if [ "`ls -la REPORT1.exp.log.Z | awk '{print $7 $6}'`" = "`date +%d%b`" ]
    then
    echo "File updated today"
    else
    echo "File not updated today"
    fi

    read dummy
    ;;

    2)clear
    etc etc etc

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    You may find it easier to do this:

    First write a script which runs just one report. You may have situations in the future where you just want to run one report. It will also make it easier to test any changes you make to this script without running all 21 reports.

    Second, let this script take the name of the report from the command line. Look for all occurrences of REPORT1 and replace them with $1. For example,
    Code:
    if [ "`ls -la REPORT1.exp.log.Z | awk '{print $7 $6}'`" = "`date +%d%b`" ]
    becomes
    Code:
    if [ "`ls -la $1.exp.log.Z | awk '{print $7 $6}'`" = "`date +%d%b`" ]
    When you run this script, you'll be putting the name of the report on the command line. For example, assuming you want to run just REPORT4, you'll say on the command line
    Code:
    myscript REPORT4
    Third, write another script which runs the first script 21 times. It can look like this:
    Code:
    #!/bin/ksh
    
    myscript REPORT1
    myscript REPORT2
    myscript REPORT3
    myscript REPORT4
    myscript REPORT5
    myscript REPORT6
    myscript REPORT7
    myscript REPORT8
    myscript REPORT9
    myscript REPORT10
    myscript REPORT11
    myscript REPORT12
    myscript REPORT13
    myscript REPORT14
    myscript REPORT15
    myscript REPORT16
    myscript REPORT17
    myscript REPORT18
    myscript REPORT19
    myscript REPORT20
    myscript REPORT21
    Or it can look like this:
    Code:
    #!/bin/ksh
    
    for single in REPORT1 REPORT2 REPORT3 REPORT4 REPORT5 REPORT6 REPORT7 REPORT8 REPORT9 REPORT10 REPORT11 REPORT12 REPORT13 REPORT14 REPORT15 REPORT16 REPORT17 REPORT18 REPORT19 REPORT20 REPORT21
    do
      myscript $single
    done
    Or it can look like this (my favorite):
    Code:
    #!/bin/ksh
    
    counter=1
    
    while [[ $counter -le 21 ]]
    do
      report_name=REPORT$counter
      myscript $report_name
      counter=$(($counter+1))
    done
    I don't see any particular advantage to using an array, since you'll be cycling through the names only once per execution.

    Hope this helps.

  3. #3
    Just Joined!
    Join Date
    Oct 2007
    Location
    Surrey, UK
    Posts
    5
    Thanks I'll have a look in to incorporating that in to my script

  4. #4
    Just Joined!
    Join Date
    Oct 2007
    Location
    Surrey, UK
    Posts
    5
    Works a treat now, I added the completed script below. Thanks again

    #!/bin/ksh
    WORKDIR=/appl/scripts/reports/logs/
    counter=1
    cd $WORKDIR
    clear

    cat << EOF


    OVERNIGHT CHECKS


    1 - Job Reports. q - Exit.

    Choose an option:
    EOF

    read ans
    case $ans in
    1)clear
    while [[ $counter –le 21 ]]
    do
    report_name=REPORT$counter
    counter=$(($counter+1))

    test -f $report_name.exp.log.Z
    if true
    then echo "$report_name exists"
    else echo "$report_name does not exist"
    fi

    test -s $report_name.exp.log.Z
    if true
    then echo "$report_name is valid file"
    else echo "$report_name is 0 size file"
    fi

    if [ "`ls -la $report_name.exp.log.Z | awk '{print $6 $7}'`" = "`date | awk '{print $2 $3]'`" ]
    then
    echo "$report_name updated today"
    else
    echo "$report_name not updated today"
    fi
    done
    read dummy
    ;;

    q|Q)clear
    echo "



    GoodBye.


    "
    sleep 2
    break;;
    *)continue
    echo "


    Try again.

    "
    sleep 3
    ;;

    esac

  5. #5
    Just Joined!
    Join Date
    Oct 2007
    Location
    Surrey, UK
    Posts
    5
    Now I just need to work out how to print the above to screen (which it does already) and also mail it out to an email address........got me stumped again lol

  6. #6
    Just Joined!
    Join Date
    Oct 2007
    Location
    Surrey, UK
    Posts
    5
    Cracked it, I created a second file for Ops to run, which runs the main script.

    #!/bin/ksh
    /fm_dir/check/report_checkscript | tee /tmp/report_out

    wait 5

    cat /tmp/report_out | mail -s "Jobs Report" ops@foo.com

    wait 5

    rm /tmp/report_out

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...