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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check if two variable are empty strings at once? (bash)
# 1  
Old 10-24-2008
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.

Code:
if [ -n "$1" ] and [ -n "$2" ];then
...

Thank you! Smilie
# 2  
Old 10-24-2008
Code:
[ "$1$2" ]|| ...

# 3  
Old 10-24-2008
Quote:
Originally Posted by radoulov
Code:
[ "$1$2" ]|| ...

I'm unable to get this to work. Could you use my example (with "if" and "then") to demonstrate this?

Thank you! Smilie

Slow...
# 4  
Old 10-24-2008
he means
Code:
[ "$1"$2" ]] || echo "both zero length"
# or
if [[ "$1" == "" && "$2" == "" ]] ; then
   echo "both zero length"
fi

# 5  
Old 10-24-2008
Awesome! Thank you both!

Here's what I did:

Code:
if [[ -n "$1" && -n "$2" ]] ; then
echo "We're good"
else
echo "We're not good"
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Stupid question to check if variable is empty

Hi, I am simply trying to check if the variable is empty in the code below but it isn;t working. Anything that I might have missed here #Check if values in job card are not empty title=`cat $filename | grep "TITLE:" | cut -d ":" -f3` if ] then echo "10:Title Empty" ":Fail">> $rptfile... (13 Replies)
Discussion started by: nua7
13 Replies

3. UNIX for Dummies Questions & Answers

Check if file is empty with variables, bash

Hello again! I have some trouble with scripting in bash. In the following script I read from a folder with the files line0_Ux.xy line1_Ux.xy line2_Ux.xy . . . Some of the files are empty. For those I would like to print a "0" in list. I think the problem with the code is that... (4 Replies)
Discussion started by: bjoern456
4 Replies

4. Shell Programming and Scripting

How to check whether a variable is empty or contains some value?

hi, i want to check whether a a variable contains some value or is empty in a shell script. so if the variable contains some value i want to do some job and if the variable doesnt contain any value then i need to skip that job. here is a sample script read_filenames.sh contains ... (5 Replies)
Discussion started by: Little
5 Replies

5. Shell Programming and Scripting

Using arrays in bash using strings to bash built-in true

I have the following code and for some reason when I call the program using /home/tcdata/tatsh/trunk/hstmy/bin/bash/raytrac.bash --cmod=jcdint.cmod I get hasArgument = hasArgument = true Somehow the array element is returning even though I have not chosen the option. ... (41 Replies)
Discussion started by: kristinu
41 Replies

6. Shell Programming and Scripting

How to check a variable for empty and a newline

I have a variable with a new line. I want to check this variable for empty or a new line Can anyone advise (4 Replies)
Discussion started by: Muthuraj K
4 Replies

7. Shell Programming and Scripting

How to check the variable is empty with spacing

How to check the variable is empty or not? aaa=" " how to check aaa variable is empty or just spacing? If only spacing inside.. it will asume it is empty. some are 6 spacing, or 8 spacing.. as long as variable is empty with spacing.. anyone can help me? (2 Replies)
Discussion started by: ryanW
2 Replies

8. 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

9. Shell Programming and Scripting

Bash-Shell: If-Clause to check if file is empty

Hello, I want to checkl whether my file has text in it or not. if ; then ... if ; then ... But none of these work Can someone help me? ---------- Post updated at 09:00 AM ---------- Previous update was at 08:55 AM ---------- The code-tags caused an displayerror,... (5 Replies)
Discussion started by: ABE2202
5 Replies

10. Shell Programming and Scripting

[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... (1 Reply)
Discussion started by: noratx
1 Replies
Login or Register to Ask a Question