[bash] Check if variable is set or blank


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [bash] Check if variable is set or blank
# 1  
Old 03-28-2008
[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.
# 2  
Old 03-28-2008
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 10:44 AM.. Reason: P.S.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How do i check if all parameters are set in bash?

Hi, I have 4 parameters passed to my shell and i validate if all four are entered using the following snippet: if then echo "Not entered" else echo "entered" fi I get the following output as 'Not entered even when i enter the values for all prompts. Please advise. Thanks. (5 Replies)
Discussion started by: Jesshelle David
5 Replies

2. Shell Programming and Scripting

To check Blank Lines, Blank Records and Junk Characters in a File

Hi All Need Help I have a file with the below format (ABC.TXT) : ®¿¿ABCDHEJJSJJ|XCBJSKK01|M|7348974982790 HDFLJDKJSKJ|KJALKSD02|M|7378439274898 KJHSAJKHHJJ|LJDSAJKK03|F|9898982039999 (cont......) I need to write a script where it will check for : blank lines (between rows,before... (6 Replies)
Discussion started by: chatwithsaurav
6 Replies

3. Shell Programming and Scripting

Bash question: working with an array of previously set variable strings

while i've used arrays to work with variables, i've never used them to loop through a set of strings and wanted to ask the community for some feedback or assistance. let me be specific. here's my code: # URL port Variables port2195=`nc -z $url2195 2195` port2196=`nc -z $url2196 2196`... (5 Replies)
Discussion started by: hungryd
5 Replies

4. Shell Programming and Scripting

Bash script set command to a variable

Hi, Will following set up work in bash script? I've got errors if assigning following binary command to a variable. But on the other hand, COMMAND="ls" works. Any explanation please? How can I assign binary command to a variable COMMAND then I can just call ${COMMAND}? COMMAND="rsync"... (3 Replies)
Discussion started by: hce
3 Replies

5. Shell Programming and Scripting

bash variable (set via awk+sed) not working as expected

Hi! Been working on a script and I've been having a problem. I've finally narrowed it down to this variable I'm setting: servername=$(awk -v FS=\/ '{ print $7 } blah.txt | sed 's\/./-/g' | awk -v FS=\- '{print $1}')" This will essentially pare down a line like this: ... (7 Replies)
Discussion started by: creativedynamo
7 Replies

6. Shell Programming and Scripting

BASH - set specific user variable via string operators

Apologies for the utter triviality of this question, but we all have to start somewhere! I've also tried searching but this question is pretty vague so I didn't (a) really know what to search for or (b) get many relevant hits to what I did search for. Anyway, I'm in the process of self-teaching... (1 Reply)
Discussion started by: u5j84
1 Replies

7. Shell Programming and Scripting

Bash : Check aphanumeric content in variable

Hello everyone, I'm trying the best way to implement a check on a variable ... in particular I need to assess the content of characters and numbers , I tried on various manuals bash scripting but I could not figure out how to do ... any help? (3 Replies)
Discussion started by: ionral
3 Replies

8. Shell Programming and Scripting

how to check the variable values is blank

HI , I have to check the values of variable is blank or not. exm : ###test test1 var=`cate filename | head -1 | cut -c1-3` I need to check the first three character of 1st line . if it is blank .then exit or we need to process . Thanks in advance . (2 Replies)
Discussion started by: julirani
2 Replies

9. Shell Programming and Scripting

How to check if two variable are empty strings at once? (bash)

I need to check if $1 or $2 are empty before continuing but I don't know if bash has any logic of the sort. This is what I'm looking for - except that "and" doesn't seem to work. if and ;then ... Thank you! :D (4 Replies)
Discussion started by: ph0enix
4 Replies

10. Shell Programming and Scripting

Check if variable is set in script

I want to check to see if a variable is set - and if not set it to something ie. variable name test I want to check if $test is set then if there is nothing set against this currently - then set it to 0 Whats the best / shortest way of doing this in a script? (3 Replies)
Discussion started by: frustrated1
3 Replies
Login or Register to Ask a Question