if $variable has whitespaces?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if $variable has whitespaces?
# 1  
Old 04-29-2010
if $variable has whitespaces?

Hi,

Here's my requirement...

There's a SFTP script with batchmode which logs in to the remote site and list all .done files to pick real files (see the following snippet)

Code:
 
echo "-ls $FTP_PATH/*.done" > temp_file
fileNames=`sftp -b temp_file $user@$ip`
isDoneFile=`echo $fileNames | sed -e "s|sftp>||g" -e "s|-ls||g" -e "s|$FTP_PATH/\*.done||g"`

Now the problem arrives I want to create a dummy file when FTP doesn't encounter .done files at remote site. In that scenario the variable fileNames will contain 'sftp> -ls /path/to/the/remote/files/*.done sftp>' value only (otherwise it would have this line and remote filenames with the path). So I replaced those things to blank using sed and did the following check

Code:
 
if [[ -z $isDoneFile ]];then
   echo "No .done files"
else
   echo ".done files are available"
fi

but even when there were no .done files it always echoed ".done files are available" because $isDoneFile had whitespaces!!

I noticed several threads regarding this here; one of them is
https://www.unix.com/shell-programmin...eld-blank.html
but not sure how to use this for my purpose? ( this could seem silly to you but any suggestions would be really helpful to me )

-dips
# 2  
Old 04-29-2010
You code is hard to read with pipes as sed delimiters. pipes have a special meaning in shell scripts.

Code:
echo "$isDoneFile" | read tmp   # this removes leading/trailing tab and space characters
if [[ -z "$tmp" ] ; then
   echo "done"
else
   echo "not done"
fi

# 3  
Old 04-29-2010
Thanks jim! as expected it worked.
-dips
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove whitespaces between delimiter and characters

Hello All, I have a pipe delimited file and below is a sample data how it looks: CS123 | | || 5897 | QXCYN87876 As stated above, the delimited files contains sometimes only spaces as data fields and sometimes there are extra spaces before/after numeric/character data fields. My requirement... (4 Replies)
Discussion started by: amvip
4 Replies

2. Shell Programming and Scripting

Separate letters and replace whitespaces

Input file: aaaa bbb dd. qqq wwww e. Output file: a a a a <s> b b b <s> d d . q q q <s> w w w w <s> e . Can I use sed to do so in one step? (5 Replies)
Discussion started by: Viernes
5 Replies

3. Shell Programming and Scripting

Retaining whitespaces...

There's an input file(input.txt) which has the following details : CBA BA <Please note the second record has a LEADING WHITESPACE which is VALID> I am using the following code to read the content of the said file line by line: while read p ; do echo "$p" done < input.txt This is the... (1 Reply)
Discussion started by: kumarjt
1 Replies

4. Shell Programming and Scripting

Removing whitespaces from a line

HI I want to remove white spaces from a line I have a file like # cat /clsep 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 I need the output like 0 1 2 3 4 . . . 15 I feel sed command cannot be used.. I went through the man page of awk command and found (7 Replies)
Discussion started by: Priya Amaresh
7 Replies

5. Shell Programming and Scripting

Remove whitespaces in the n first characters?

I assume removing whitespaces in the n first characters of a string would be an easy task for sed? If so, how? (7 Replies)
Discussion started by: KidCactus
7 Replies

6. Shell Programming and Scripting

Substring in string with whitespaces

hello, i have this string: variable="1234 /PARAMETER_1:text /PARAMETER_2:othertext" i tried to do expr match $variable '.*\(*\)' but i keep getting expr error i need to extract the word text... thank you (4 Replies)
Discussion started by: Aloush89
4 Replies

7. UNIX for Dummies Questions & Answers

Using grep to find multiple whitespaces

Hi All, Have a query on finding the lines with at least one whitespace. Have following lines in my file myfile.txt I I LOVE I LOVE FREEDOM I LOVE FREEDOM I LOVE FREEDOM AND PEACE I ran the following query to get the lines with at least 3 words. grep '\(* +\)\{3,\}' myfile.txt... (6 Replies)
Discussion started by: sh_kk
6 Replies

8. Shell Programming and Scripting

replace nulls with whitespaces in a file

Hi, i have a file which contains data in fixed length. each row contains data of 10 characters fixed length. The data in the file appears like 4567782882 some times i may recieve dat less than fixed length of 10. in such a case i find nulls appended at the trailing spaces when i do a... (2 Replies)
Discussion started by: meeragiri
2 Replies

9. UNIX for Dummies Questions & Answers

trimming whitespaces in a file

Hello, Im trying write a bash sript to search a file and identify any file names that contain a space at the end of them. I was trying to use the grep command but I can find how I would identify the character I'm looking for. Any help would be grately appreciated. (2 Replies)
Discussion started by: gintreach
2 Replies

10. Shell Programming and Scripting

help me to remove the whitespaces between the lines ...urgent

Hello all, i have a problem. please help me to remove the white spaces and tabs betweeen line. i.e., file1 contains some text.. text starts_hdsffdsd sdfsddssdds******** sdfsdsd*********** sdfsdsdfsdsdfsdsds*** ****fsd_test_ends one or 2 blank lines (* indicates white spaces or tabs) ... (5 Replies)
Discussion started by: kumar1
5 Replies
Login or Register to Ask a Question