Unix/Linux Go Back    

Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

[bash] Check if variable is set or blank

Shell Programming and Scripting

Thread Tools Search this Thread Display Modes
Old Unix and Linux 03-28-2008   -   Original Discussion by noratx
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:

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
Old Unix and Linux 03-28-2008   -   Original Discussion by noratx
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 11 Times in 9 Posts

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 10:44 AM.. Reason: P.S.
Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 07:24 AM
Bash : Check aphanumeric content in variable ionral Shell Programming and Scripting 3 11-24-2009 06:41 AM
how to check the variable values is blank julirani Shell Programming and Scripting 2 12-06-2008 05:24 PM
How to check if two variable are empty strings at once? (bash) ph0enix Shell Programming and Scripting 4 10-24-2008 03:18 PM
check for the first character to be blank anthreedhr UNIX for Dummies Questions & Answers 7 10-22-2003 11:05 AM

All times are GMT -4. The time now is 05:42 PM.