Although the test [ -z $line ] will work as long as no more than one word was typed in; it isn't doing what most people expect.
If an empty line is entered, it will expand to [ -z ] which will yield TRUE because "-z" is not an empty string (not because $line expands to an empty string).
If a single word is entered (and it does not contain any pattern matching characters that would match more than one file in the current directory), then it will expand to [ -z word ] which will evaluate to TRUE as expected.
If more than one word is entered or any pattern matching characters in a single word expand to a list of more than one filename, you will either get a syntax error or some random result depending on what the 1st word is and, if there are more than two words (after pattern matching), it will also depend on the subsequent words.
To use this form reliably, the line:
should be changed to:
If an empty line is entered, it will expand to [ -z ] which will yield TRUE because "-z" is not an empty string (not because $line expands to an empty string).
If a single word is entered (and it does not contain any pattern matching characters that would match more than one file in the current directory), then it will expand to [ -z word ] which will evaluate to TRUE as expected.
Now i belived -z is testing if argument is empty.
Thus, expecting that an empty line (variable in general) returns true.
However, i got the 'multiple expanding part'...
So in conclusion, the quotes around make it less error prone, but it doesnt matter whether or not the variable contains spacechar.
Truely a standards guy you are, me gotta get used again to do quotes around there again
Thank you every one for sharing and helping , all codes which you provided to me working fine , now I'am confused with which one I go with. I like the one provided by sea because it is the shorter one. Thank you Don for the info
Thank you every one for sharing and helping , all codes which you provided to me working fine , now I'am confused with which one I go with. I like the one provided by sea because it is the shorter one. Thank you Don for the info
As long as you only use bash as your shell, the code sea suggested (with the expansion of $line inside double quotes) will do what you want:
The -p option to the read shell built-in is bash specific. The read -p option in ksh means something very different (read from a co-process), and in many other shells it would just be rejected as an unknown option. The equivalent Korn shell-specific code would be:
If you only want to use constructs that will work with any shell that is based on Bourne shell syntax (which will include the default shell on any Linux or UNIX system), try:
Could be even shorter:
Is this sort of parameter expansion a ksh speciality? I wonder why you prefer those awkward if/else constructs...?
No, ${line:=Kim} is not ksh-specific. It is supported by any shell that meets the POSIX requirements for shell parameter expansions. It will not, however, work with a pure Bourne shell (such as /bin/sh on Solaris systems).
Note, however, that as I said before, read -p is bash-specific and will not work with ksh.
Last edited by Don Cragun; 09-23-2014 at 03:06 PM..
Reason: Add note.
I have a below syntax its working fine...
var12=$(ps -ef | grep apache | awk '{print $2,$4}')
Im getting expected output as below:
printf "%b\n" "${VAR12}"
dell 123
dell 456
dell 457
Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Hi All,
I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Hi all,
Hereby wish to have your advise for below:
Main concept is
I intend to get current directory of my script file.
This script file will be copied to /etc/init.d.
A string in this copy will be replaced with current directory value.
Below is original script file:
... (6 Replies)
can anyone please help me with this:
i have written a shell script and a stored procedure which has one OUT parameter. now i want to use that out parameter as an input to the unix script but i am getting an error as variable not found. below are the unix scripts and stored procedure...
... (4 Replies)
Hi,
I am trying to do the following thing
var='date'
$var
Above command substitutes date for and in turn runs the date command and i am getting the todays date value.
I am trying to do the same thing as following, but facing some problems,
unique_host_pro="sed -e ' /#/d'... (3 Replies)
Dear all,
I have basic knowledge of Unix script and her I am trying to process variable length and variable format CSV file.
The file length will depend on the numbers of Earnings/Deductions/Direct Deposits.
And
The format will depend on whether it is Earnings/Deductions or Direct Deposits... (2 Replies)
Hi all,
I have a variable say var1 (output from somewhere, which I can't change)which store something like this:
echo $var1
name=fred
age=25
address="123 abc"
password=pass1234
how can I make the variable $name, $age, $address and $password contain the info?
I mean do this in a... (1 Reply)
I want to instert Category:XXXXX into the 2. line
something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong:
sed "2i\\${n}Category:$cat\n"
Sample:
Titel Blahh Blahh abllk sdhsd sjdhf
Blahh Blah Blahh
Blahh
Should look like... (2 Replies)
Hello,
i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem.
my var1 is a string constructed like this:
filename1 filerev1 filepath1
my var2 is another string constructed like this:
filename2 filerev2 filepath2
when i do... (2 Replies)