[bash] Check if variable is set or blank | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

[bash] Check if variable is set or blank

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-28-2008
noratx noratx is offline
Registered User
 
Join Date: Mar 2008
Last Activity: 24 January 2011, 11:03 AM EST
Posts: 40
Thanks: 2
Thanked 0 Times in 0 Posts
[bash] Check if variable is set or blank

Hello guys.
In my script, i have the following code:

Code:
echo "The tarfile contains these directorys"
tar -tf file.tar > tarlist.txt
cat tarlist | awk -F/ '{print $1 "/" $2}' | nl
echo "Enter path of the directory you want to extract or just press enter to extract everything: "
read path
/usr/local/bin/gtar -xf file.tar $path

I need to check if the user gave a path to the script, or if he just pressed enter?
I know what to do between the 'THEN' and 'ELSE' statements.
I just need to know how to check if $path is really set or not.
Sponsored Links
    #2  
Old 03-28-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 10 Times in 8 Posts

Code:
case $path in '') echo not set;; *) echo is set;; esac

If you want to distinguish between not set and set but empty, have a look at the ${var-value} and ${var:-value} construct.

As long as you are not quoting $path, you don't need to make things conditional, though.. Your code should work already. (On the other hand, you properly ought to put $path in double quotes.)

PS. You are extracting into tarlist.txt, but using tarlist without the .txt. Also the cat | awk is a Useless Use of Cat. Google for that phrase.

Last edited by era; 03-28-2008 at 09:44 AM.. Reason: P.S.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
check if return value is blank Satyak Shell Programming and Scripting 1 12-26-2009 06:24 AM
Bash : Check aphanumeric content in variable ionral Shell Programming and Scripting 3 11-24-2009 05:41 AM
how to check the variable values is blank julirani Shell Programming and Scripting 2 12-06-2008 04:24 PM
How to check if two variable are empty strings at once? (bash) ph0enix Shell Programming and Scripting 4 10-24-2008 02:18 PM
check for the first character to be blank anthreedhr UNIX for Dummies Questions & Answers 7 10-22-2003 10:05 AM



All times are GMT -4. The time now is 11:26 AM.