How to check a variable for empty and a newline


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check a variable for empty and a newline
# 1  
Old 02-02-2010
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
# 2  
Old 02-02-2010
Code:
if [ -z "`echo "$VARIABLE" | tr -d '\n'`" ]; then
    ...
fi

Is this what you mean?
# 3  
Old 02-02-2010
Code:
case $var in 
  ""|*"\n"*) echo hello
esac

# 4  
Old 02-03-2010
Quote:
Originally Posted by Scrutinizer
Code:
case $var in 
  ""|*"\n"*) echo hello
esac


You are testing for a literal '\n', not a newline.

Code:
NL='
'
case $var in 
  ""|*"$NL"*) echo hello
esac

# 5  
Old 02-03-2010
Oww, that's right, thanks!\n Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert newline when grep result is empty

Given a csv file with 40 columns with name, address, hometown etc. I use a bash command in 1 line which: 1. gets the address column and pipes that to 2. grep the first digit and everything that follows Command: awk -F ";" '{print $19}' /Users/jb/Desktop/ReorderTempTotal.csv | grep -o "\d.*"... (7 Replies)
Discussion started by: JBVeenstra
7 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. Shell Programming and Scripting

Cant check empty string

Hello So i have that script collection, in which i have a single script to create a configuration file. In there, i have multiple occourences of something like this: prj_title=$(tui-read "What is the TITLE? ($prj_name):") ] && prj_title="${prj_name/_/ }" They all work as expected, if... (5 Replies)
Discussion started by: sea
5 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

Check if the string is empty

I am reading from a file and executing the jobs with/without parameters as the job requires. File job1 R job2 job3 Y 123 if then <job>.ksh else <job>.ksh $params fi This works fine if the line read from the file has parameters it executes like job1.ksh R But for... (2 Replies)
Discussion started by: nw2unx123
2 Replies

6. Shell Programming and Scripting

How to check newline character in file?

Hi All, I have file with only one record,always be only one record. as like below. if that line contains newline end of the line..no need to add, if not just add the new line character. END OF FILE. ROW COUNT: 7 Please help me.. Thanks, (9 Replies)
Discussion started by: bmk
9 Replies

7. Homework & Coursework Questions

Check whether a Directory is empty or not

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 1.pls tell me the command for checking whether a given directory is empty or not . 2. can i check what is the... (1 Reply)
Discussion started by: upvan111
1 Replies

8. UNIX for Dummies Questions & Answers

newline character in a variable

variable="unix\nlinux" echo $variable expected output: unix linux :wall: can i do that ?? thanks in advance!! (3 Replies)
Discussion started by: sathish92
3 Replies

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

10. 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
Login or Register to Ask a Question