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?
# 1  
Old 04-07-2012
Network My if statement doesn't work why?

I have the following and for some reason I can't have two options together.
I mean if I choose -u and -p it won't work... why?
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

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)    priority=OPTARG;[ 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 [ $user ] && [ $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 [ 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

# 2  
Old 04-07-2012
I didn't look very closely, but it looks like you are missing a dollar sign in this statement:

Code:
    p)    priority=OPTARG;[ OPTIND=${OPTIND} ];;

Should be (I think):

Code:
    p)    priority=$OPTARG;[ OPTIND=${OPTIND} ];;

I didn't look to see if this was your only issue, but it is where I would start. Change and have a go with it.
# 3  
Old 04-07-2012
Thanks for taking your time on this.
When I run:
Quote:
$./myscript -u root data.txt
this is what I get as the output is:
Quote:
TOP ANALYSIS REPORT

Name: root
-------------------------------------------------------------------------------------------------
| USER | PROCESS | PROC ID | % CPU | % MEM | PRIORITY |
|-----------------+-------------------------+------------+------------+------------+------------|
| root | init | 1 | 0.0 | 0.1 | 20 |
| root | migration/0 | 1008 | 2.0 | 1.8 | 1 |
| root | watchdog/0 | 5 | 0.0 | 0.0 | 1 |
-------------------------------------------------------------------------------------------------
But it shouldn't give the priority column without -p command in the terminal. do you know why I get this??

Last edited by bashily; 04-07-2012 at 06:39 PM.. Reason: correction!
# 4  
Old 04-07-2012
You need to dereference your variables when you use them as a part of a test expression.

Code:
if [ $user ]; then

and
Code:
if [ $pflag ]; then

Both need to have $varname and not just varname.
# 5  
Old 04-07-2012
So, you mean something like this:
Code:
if [ $user ]; then
    printf "\nTOP ANALYSIS REPORT\n"
    printf "\nName: %4s\n" $user

    if [ $mycommand ]; 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 "-------------------------------------------------------------------------------------------------"
 fi
  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

Believe me, I tried this as well. It doesn't work. I am totally lost! Smilie
This is the output:
Quote:
TOP ANALYSIS REPORT

Name: root
# 6  
Old 04-07-2012
That looks correct if you only have -u and -p (at least, since you re-structured the if statements...).
# 7  
Old 04-07-2012
I am going to cry my eyes out! this damn script doesn't work and I can't see my mistakes! I think everything is OK but if user choose two options, the output won't be correct!Smilie
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