LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   test statement in bash (https://www.linuxquestions.org/questions/linux-newbie-8/test-statement-in-bash-907794/)

casperdaghost 10-12-2011 11:26 AM

test statement in bash
 
I initialized the todays_CASH_session to null and hope that if the variable after the for loop was still null that it would go to the last stetement for user to check the engine.


is the test -n the right tool to use, when i run the command raw from the terminal i get nothing.
(/data/execCmd flight05 /RASH/STATUS | grep Session | cut -d" " -f2 | sort -u

so it should be null in script after for loop. but the script is acting line it has a value.


Code:

#!/bin/sh

todays_CASH_session=''
for ((i=0; i<1 ; i++))
do
todays_CASH_session=$(/data/execCmd light05 /CASH/STATUS | grep Session | cut -d" " -f2 | sort -u
)
done


if [ -n $todays_CASH_ session ]; then
        echo "Todays session is $todays_CASH_session"
        echo
        echo " Do you want to stop this session (y/n)"
        read $REPLY
        if [ "$REPLY" == "y" ]; then
                echo "Stopping the CASH session $todays_RASH_session"
                sudo -A /data/execCmd light05 /CASH/STOP $todays_CASH_session
                continue
        else
                echo "Not going to stop CASH Session :$todays_CASH_session"
                exit
        fi
        echo "Do you want to bounce this session (y/n)"
        read $BOUNCEREPLY
        if [ "$BOUNCEREPLY" == "y" ]; then
                echo "Bouncing CASH Engine"
                sudo /data/bounce light05
        else
                echo "Not bouncing the CASH Engine"
                exit
        fi
else
        echo "Please check to see that engine is running"

fi


casperdaghost 10-12-2011 11:31 AM

do i need the exit statement at the end of the if statments ?

lonesoac0 10-12-2011 12:15 PM

You only need an exit statement at the end of the script.

PTrenholme 10-12-2011 12:33 PM

When you set todays_CASH_session, the loop runs from i=0 while i < 1, so you will always execute the value setting once.

That is, this section:
Code:

todays_CASH_session=''
for ((i=0; i<1 ; i++))
do
todays_CASH_session=$(/data/execCmd light05 /CASH/STATUS | grep Session | cut -d" " -f2 | sort -u
)
done

is equivalent to
Code:

todays_CASH_session=$(/data/execCmd light05 /CASH/STATUS | grep Session | cut -d" " -f2 | sort -u)
So, are you just asking why /data/execCmd light05 /CASH/STATUS | grep Session | cut -d" " -f2 | sort -u returns a non-null string?:scratch:

Can't you just run the command in a terminal window and look at the output?

casperdaghost 10-12-2011 12:40 PM

I was hoping if the -- /data/execCmd light05 /CASH/STATUS | grep Session | cut -d" " -f2 | sort -u -- returned no value the program would skip to bottom -- echo "Please check to see that engine is running". However even when i run it from the command line and get nothing back, i run the script and the internal if statements are run.

I do not know if -n is the right tool for the job.

casperdaghost 10-12-2011 01:03 PM

Yeah so I am not asking why the todays_CASH-session returns an null string. my concern is that when the return values is nothing, the assignment is null, why are the nested if statments running.

is -n the right tool for testing the statement.

are the if statements nested wrong.

becasue it the todays_CASH_session is null, it should jump to bottom of program

rknichols 10-12-2011 01:44 PM

Quote:

Originally Posted by casperdaghost (Post 4496633)
Code:

if [ -n $todays_CASH_ session ]; then

You've got two problems in that line. First, you've got an extra space before the word "session". Second, when you are testing a possibly null variable, you need to quote the variable so that the test command can see an explicitly null argument as opposed to a missing argument. What you've got there will actually expand to
Code:

[ -n  session ]
which is just testing whether the literal word "session" is non-null (presuming that there is no actual variable named "todays_CASH_"). The line should be
Code:

if [ -n "$todays_CASH_session" ]; then

casperdaghost 10-12-2011 02:24 PM

DEAD_ON_TARGET

quoting the variable - thanks.

David the H. 10-13-2011 07:16 AM

Is there any special reason why you're using #!/bin/sh here? If you don't need posix-compliant portability, then you should probably switch it to /bin/bash and take advantage of its more advanced features, such as the [[ test.

http://mywiki.wooledge.org/BashFAQ/031

And as you've discovered, you should always quote your variable and expansions and command substitutions, unless you want word-splitting to occur.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

This is particularly important inside the old [ test (but not [[, see the link for details).


All times are GMT -5. The time now is 12:55 AM.