LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-12-2011, 12:26 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
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
 
Old 10-12-2011, 12:31 PM   #2
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
do i need the exit statement at the end of the if statments ?
 
Old 10-12-2011, 01:15 PM   #3
lonesoac0
Member
 
Registered: Jan 2010
Distribution: Ubuntu
Posts: 94

Rep: Reputation: 4
You only need an exit statement at the end of the script.
 
Old 10-12-2011, 01:33 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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?

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

Last edited by PTrenholme; 10-12-2011 at 01:37 PM. Reason: Typo
 
Old 10-12-2011, 01:40 PM   #5
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
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.

Last edited by casperdaghost; 10-12-2011 at 01:44 PM.
 
Old 10-12-2011, 02:03 PM   #6
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
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
 
Old 10-12-2011, 02:44 PM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,968

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Quote:
Originally Posted by casperdaghost View Post
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
 
1 members found this post helpful.
Old 10-12-2011, 03:24 PM   #8
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
DEAD_ON_TARGET

quoting the variable - thanks.
 
Old 10-13-2011, 08:16 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] what does =~ means in if test statement. saini_mw Programming 3 05-09-2011 02:15 PM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 07:00 AM
EASY scripting question - trying to figure out a test statement Elendrael Linux - Newbie 1 09-16-2007 08:52 PM
LXer: Defence statement released in Israeli GPL test LXer Syndicated Linux News 0 04-03-2007 04:31 PM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 06:20 PM


All times are GMT -5. The time now is 09:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration