space in variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting space in variable
# 1  
Old 11-20-2005
space in variable

Hi,

How to check if a variable is having spaces or empty in shell scripts.

Please help

esham
# 2  
Old 11-20-2005
For emptiness, man test says

Code:
       [-n] STRING
              the length of STRING is nonzero

       -z STRING
              the length of STRING is zero

There are other constructs which allow to set variables if they are not set et al..
Code:
       ${parameter:-word}
              Use  Default  Values.  If parameter is unset or null, the expan-
              sion of word is substituted.  Otherwise, the value of  parameter
              is substituted.
       ${parameter:=word}
              Assign  Default  Values.   If  parameter  is  unset or null, the
              expansion of word is assigned to parameter.  The value of param-
              eter  is  then  substituted.   Positional parameters and special
              parameters may not be assigned to in this way.
       ${parameter:?word}
              Display Error if Null or Unset.  If parameter is null or  unset,
              the  expansion  of  word (or a message to that effect if word is
              not present) is written to the standard error and the shell,  if
              it is not interactive, exits.  Otherwise, the value of parameter
              is substituted.
       ${parameter:+word}
              Use Alternate Value.  If parameter is null or unset, nothing  is
              substituted, otherwise the expansion of word is substituted.

# 3  
Old 11-20-2005
The problem is that the variable is getting by reading each line from a file.
so some times, the variable reads the line where there is no charectors..

In this case, the variable takes as a line full of spaces..
I need to test if the variable is full of spaces..

please help
# 4  
Old 11-20-2005
Look at this

Code:
sh-2.05b$ cat esham.txt
a
b

c
d
  
e

Code:
sh-2.05b$ while read line; do if [[ "$line" == "" ]] ; then echo "$line"; fi ; done < esham.txt 


sh-2.05b$

# 5  
Old 11-20-2005
okay, it worked..
the problem was with some other iterations in the script..

thanks for ur help..
esham
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Space in PATH variable

Hi All, I did a review of some threads to see if someone had come across this problem. My system is an oracle virtual box solaris 10 installed on a windows 10 system. Right now I am learning scripting and am just playing with the env variables. I am trying to update the PATH to... (9 Replies)
Discussion started by: bdby
9 Replies

2. Shell Programming and Scripting

How to remove tab space if any in a variable?

I have a variable sumOfJEOutputFile which is the output file of an SQL command which contains the output of that SQL. The output looks like below: ----------- 58 I am using following code to manipulate the output: (sed 1,2d $sumOfJEOutputFile > $newTemp1 | sed '$d' $newTemp1)... (4 Replies)
Discussion started by: Sharma331
4 Replies

3. Shell Programming and Scripting

Finding position of space in a variable

HI All, am trying to find the position of space in a variable, it is working for other characters other than space ulab="ulab1|ulab2" find_pos=`expr index $ulab '|'` echo $find_pos above code worked fine but below one says syntax error ulab="ulab ulab2" find_pos=`expr index $ulab ' '`... (2 Replies)
Discussion started by: ulab
2 Replies

4. UNIX for Dummies Questions & Answers

awk for variable with a space

Hi experts, why does $ echo "one two three" | awk '{$3="my tree"; print $0}' one two my tree work, but $ var="my tree" $ echo "one two three" | awk '{$3="'$var'"; print $0}' awk: {$3="my awk: ^ unterminated string does not work? How can the variable tha contains a space be... (2 Replies)
Discussion started by: GermanicGalore
2 Replies

5. Shell Programming and Scripting

Setting path variable with a space.

Hi I am using MKS Toolkit c shell. I am trying to set a path variable something like c:/Program Files/blah/blah so set path=(c:/Program Files/blah/blah) this, however, does not work as it splits this thing up into 'c:/Program' and 'Files/blah/blah'. Does anyone have any ideas on... (9 Replies)
Discussion started by: vas28r13
9 Replies

6. Shell Programming and Scripting

Preserve space in variable of AWK

This seems to be a stupid basic question, but I cant get the space to stick in the awk variable. I do use this command to grep a time range of the log file. cat /var/log/daemon.log | awk '$0>=from&&$0<=to' from="$(date +%b" "%e" "%H:%M:%S -d -24hour)" to="$(date +%b" "%e" "%H:%M:%S)" I now... (9 Replies)
Discussion started by: Jotne
9 Replies

7. Shell Programming and Scripting

Using Variable With Space In It?

Hey , I'm new to Bash and am having trouble with writing a script. I have a variable and want to use it to create a tar file, but, I am having problems. Can anyone tell me what I'm doing wrong? filestr=`date "+%y-%m-%d %H.%M.%S"`.tar tar -cvf $filestr test.txt So, basically,... (3 Replies)
Discussion started by: beefeater267
3 Replies

8. Shell Programming and Scripting

appending space to variable

Hi I need to write a script where there the user enters 3 input parameter variable number the program should ask the user left or right if it is left , the number specified that many spaces should be added to the value in front of the value and saved in the samee variable itself and if it is... (5 Replies)
Discussion started by: viv1
5 Replies

9. Shell Programming and Scripting

how to merge two variable in one variable with space between them?

Dear all I had two separate variable. Now i want to make them merge into one new variable with space between them. Kindly suggest me. var1=dec 15 var2=10 i want var3=dec 15 10 My main aim is as below: op of date command: >date >Sat Dec 15 10:17:35 IST 2007 i want only Dec... (2 Replies)
Discussion started by: jaydeep_sadaria
2 Replies

10. Shell Programming and Scripting

How to trim space in output variable ?

Hi , I have a code like this: uid=scott password=tiger database=db01 cat >runid_val.sql<<-EOA SET ECHO OFF SET FEEDBACK OFF SET HEADING OFF SELECT trim(runid_seq.nextval) FROM dual; EXIT EOA echo `cat runid_val.sql` V_RUNID=`sqlplus -s $uid/$password@$database @runid_val.sql`... (5 Replies)
Discussion started by: vj_76
5 Replies
Login or Register to Ask a Question