My if statement doesn't work why?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting My if statement doesn't work why?
# 8  
Old 04-07-2012
I had a closer look at your script (before I only looked at the statements that you pointed out). The problem is that you are testing pflag but never setting it. Your statement when gathering options probably should be:

Code:
 p)    pflag=1;  [ OPTIND=${OPTIND} ];;

If I were writing the script, I would initialise it to zero, and specifically test for it being greater than zero which would work with the above code.

Code:
if [ $pflag -gt 0 ]        # -p was set on command line

I also think that you need to quote your variables when testing using -n or -z within single brackets. For example:

Code:
if [ -z "$user" ]

I also prefer to specifically use -n when testing to see if a string (contents of a variable) has size:

Code:
if [ -n "$user" ]

even though the following might work in some shells:

Code:
if [ $user ]

Hope this gets you further.
# 9  
Old 04-08-2012
Thanks
# 10  
Old 04-08-2012
Implementing the suggestions I made before, this works fine for me:

Code:
#!/bin/bash

resetTime=1
mytotalTime=0
totalHour=0
totalMin=0
averagemem=0
finalaverage=0
times=0
function usage
{
    cat << EOF
    USAGE: $0 [options] file
EOF
}

function usageOption
{
    cat << EOF
    $0 Must select report type: -u(ser) or -c(ommand)
EOF
}

function displayTime
{
    awk -F"," '/'$user'/{print $7}' $file > /tmp/mytotalTime.$USER
    cat /tmp/mytotalTime.$USER | while read resetTime;
    do
        Min=$(echo $resetTime | cut -d ":" -f2)
        Hour=$(echo $resetTime | cut -d ":" -f1)
        totalHour=$((totalHour+Hour))
        echo $totalHour > /tmp/totalHour.$USER
        totalMin=$(echo "($totalMin+$Min)" | bc)
        echo $totalMin > /tmp/totalMin.$USER
    done
    totalHour=$(cat /tmp/totalHour.$USER)
    totalMin=$(cat /tmp/totalMin.$USER)
    mytotalTime=$(echo "($totalHour*60+$totalMin)" | bc)
    totalHour=$(echo "($mytotalTime/60)" | bc)
    totalMin=$(echo "($mytotalTime%60)" | bc)
    echo -e "TOTAL RUNNING TIME:    " $totalHour":"$totalMin "\n"
}

if [ $# -eq 0 ]; then
    usage
    exit 1
fi

pflag=
user=""
command=
fileName=
totalTime=
memoryUse=
priority=
OPTIND=1    # Reset in case getopts has been used previously in the shell

while getopts "u:c:f:tmp" opt;do
 case $opt in
    u)    user=$OPTARG;[ OPTIND=${OPTIND} ];uflag=$user;;
    c)    mycommand=$OPTARG;[ OPTIND=${OPTIND} ]; echo $OPTIND;;
    f)    fileName=$OPTARG;echo "$fileName";[ OPTIND=${OPTIND} ]; echo $OPTIND;;
    t)    totalTime=$OPTARG;[ OPTIND=${OPTIND} ]; echo $OPTIND;;
    m)    memoryUse=$OPTARG;echo "$memoryUse";[ OPTIND=${OPTIND} ];;
    p)    pflag=1;  [ OPTIND=${OPTIND} ];;
    \?)    usage;exit 1;;
    *)        echo -e "\nOption -$OPTARG requires an argument.\n";exit 1;;
 esac
done
shift $(($OPTIND - 1))

# Check for proper number of command line args.

EXPECTED_ARGS=1
#E_BADARGS=65

if [ $# -ne $EXPECTED_ARGS ]; then
    usage
    exit 1
 #exit $E_BADARGS
fi

if [ -n "$user" ] && [ -n "$mycommand" ]; then
    usageOption
    exit 1

elif [ ! -f $1 ]; then
    usage
    exit 1
elif [ $# -ge 2 ]; then
    usage
    exit 1
elif [ -z "$user" ] && [ -z "$mycommand" ]; then
    usageOption
    exit 1
fi

file=$1

if [  -n "$user" ]
then
    printf "\nTOP ANALYSIS REPORT\n"
    printf "\nName: %4s\n" $user

    if [ $pflag  ]; then        ###
        echo "-------------------------------------------------------------------------------------------------"
        printf "| %-15s | %-23s | %-10s | %-10s | %-10s | %-10s |\n" "USER" "PROCESS" "PROC ID" "% CPU" "% MEM" "PRIORITY"
        echo "|-----------------+-------------------------+------------+------------+------------+------------|"
        awk -F"," '/'$user'/ { printf "| %-15s | %-23s | %-10s | %-10s | %-10s | %-10s |\n", $2, $3, $1, $5, $6, $4 }' $file
        echo "-------------------------------------------------------------------------------------------------"
    else
        echo "------------------------------------------------------------------------------------"
        printf "| %-15s | %-23s | %-10s | %-10s | %-10s |\n" "USER" "PROCESS" "PROC ID" "% CPU" "% MEM"
        echo -e "|-----------------+-------------------------+------------+------------+------------|"
        awk -F"," '/'$user'/ { printf "| %-15s | %-23s | %-10s | %-10s | %-10s |\n", $2, $3, $1, $5, $6 }' $file
        echo -e "------------------------------------------------------------------------------------"
   fi
fi

This User Gave Thanks to agama For This Post:
# 11  
Old 04-08-2012
THANK YOU SOOOOO MUCH!!!! You are the best. I have nothing to say. WELL DONE!!SmilieSmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exit 1 doesn't work

Hi tail -f $PROGPATH/NBU_pgbaserestore_$1.log | while read LOGLINE do if ] && ! ] then date "+%d.%B.%Y %H:%M:%S" echo "ERROR: NBU" echo "$LOGLINE" TAILKILL=$(pgrep -P $$ -x tail) kill -9 $TAILKILL exit 1 ... (3 Replies)
Discussion started by: kvaikla
3 Replies

2. Shell Programming and Scripting

-ne 0 doesn't work -le does

Hi, I am using korn shell. until ] do echo "\$# = " $# echo "$1" shift done To the above script, I passed 2 parameters and the program control doesn't enter inside "until" loop. If I change it to until ] then it does work. Why numeric comparison is not working with -ne and works... (3 Replies)
Discussion started by: ab_2010
3 Replies

3. Shell Programming and Scripting

How come this if statement doesn't work?

greetings, the following code isn't working as i expect it to. the first dbl brackets do but the second set gets ignored. ie: if i'm on t70c6n229 it echoes "Something" and i expect it not to. what am i missing? if " ]] || " ]]; then echo "Something" fi thanx! (9 Replies)
Discussion started by: crimso
9 Replies

4. UNIX for Dummies Questions & Answers

Why doesn't this work?

find . -name "05_scripts" -type d -exec mv -f {}/'*.aep\ Logs' {}/.LogFiles \; Returns this failure: mv: rename ./019_0120_WS_WH_gate_insideTEST/05_scripts/*.aep\ Logs to ./019_0120_WS_WH_gate_insideTEST/05_scripts/.LogFiles/*.aep\ Logs: No such file or directory I don't know why it's trying... (4 Replies)
Discussion started by: scribling
4 Replies

5. Solaris

shutdown -y -i5 -g0 DOESN'T work

hello, The command above seems not working on my solaris 8/9 sparc machines. a. resulted to the ff below when I instead use "shutdown" only. Broadcast Message from root (pts/1) on "hostname" date.. The system "hostname" will be shut down in 30 seconds THE SYSTEM bdosg IS BEING SHUT... (4 Replies)
Discussion started by: lhareigh890
4 Replies

6. Shell Programming and Scripting

bc: scope doesn't work for me

I am trying to use bc to calculate the difference between two nano second time stamps. bc does the calculation but seems to ignore the scale option: micro_start=$(date +%s.%N) # .. some stuff happens here micro_stop=$(date +%s.%N) TOT=$(echo "scale=3; $micro_stop - $micro_start" | bc)... (2 Replies)
Discussion started by: LostInTheWoods
2 Replies

7. UNIX for Dummies Questions & Answers

lp -o <option> doesn't work

why lp -o <option> doesn't work on my SCO Unix. (4 Replies)
Discussion started by: wendyz
4 Replies

8. UNIX for Dummies Questions & Answers

Script doesn't work, but commands inside work

Howdie everyone... I have a shell script RemoveFiles.sh Inside this file, it only has two commands as below: rm -f ../../reportToday/temp/* rm -f ../../report/* My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
Discussion started by: cheongww
2 Replies

9. Shell Programming and Scripting

Why doesn't this work?

cat .servers | while read LINE; do ssh jason@$LINE $1 done exit 1 ./command.ksh "ls -l ~jason" Why does this ONLY iterate on the first server in the list? It's not doing the command on all the servers in the list, what am I missing? Thanks! JP (2 Replies)
Discussion started by: jpeery
2 Replies

10. UNIX for Dummies Questions & Answers

my case statement doesn't work..

CO UNixware 7.1.1 Hi friends, I have chopped my case statementt out of my .profile and put it in another script called setsid. The case statement works when run from my .profile but not from my setsid file. All that the script does is set an environmental variable based on user input.... (7 Replies)
Discussion started by: sureshy
7 Replies
Login or Register to Ask a Question