Script stops running after assigning empty string for a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script stops running after assigning empty string for a variable
# 1  
Old 10-11-2011
Script stops running after assigning empty string for a variable

Hi,

This is the first time I see something like this, and I don't why it happens.
Please give me some help. I am really appreciate it.

Basically I am trying to remove all empty lines of an input..

Code:
#!/bin/bash

set -e
set -x

str1=`echo -e "\nhaha" | grep -v ^$`
#str2=`echo -e "\n" | grep -v ^$`
echo "done"

This works fine for str1 (aka I can see "done" as output)
However, if I use str2, after assigning empty string for str2. The script stops there. And I do not see any output at all.

Thanks a lot in advance.
yoyomano
# 2  
Old 10-11-2011
try to echo the values of str1 and str2.. It works for me ..
Code:
$ cat filename
str1=`echo -e "\nhaha" | grep -v ^$`
echo "STR1: $str1"
str2=`echo -e "\n" | grep -v ^$`
echo "STR2: $str2"
[ "$str2" = "" ] && echo "str2 is NULL" || echo "str2 not NULL"
echo "done"
$
$ bash filename
STR1: haha
STR2:
str2 is NULL
done
$

# 3  
Old 10-11-2011
I have managed to use sed instead of grep.

I just change from
Code:
grep -v ^$

to
Code:
sed '/^$/d'

However, I will be really grateful if someone can explain why grep doesn't work.

Thanks a lot in advance.,
# 4  
Old 10-11-2011
Quote:
Originally Posted by yoyomano
I have managed to use sed instead of grep.
However, I will be really grateful if someone can explain why grep doesn't work.

Thanks a lot in advance.,
grep is actually working..why do you think so?
Code:
echo -e "\n"|grep -v ^$

is equal to
Code:
echo -e "\n"|sed '/^$/d'

both of these give same result so is NULL.
# 5  
Old 10-11-2011
The exit code of the assignment is the exit code of the last command of the pipe line which is grep. Since grep didn't find any matching data, it exited with 1. The shell script exited since you have set -e at the beginning.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning any number to the variable in cshell script

Hello Guys, I would like to ask you for a favor. Could you please help me how can I assign any number as the parameter to a, from stdin (-c), in the following command line by using the 'switch' in a script? awk '$8>a {print "File name:" $5,$8}' I would also appreciate if you can share any... (1 Reply)
Discussion started by: Padavan
1 Replies

2. Solaris

Assigning an expression to a variable in shell script

i am trying to assign the following expression to a variable in Unix shell script and want to use that variable in some other expression. But unable to get the required thing done. Please help with this.... This is the expression which i want to provide as input the variable date '+%y:%m:%d' |... (3 Replies)
Discussion started by: ssk250
3 Replies

3. Shell Programming and Scripting

Assigning variable using script

Hi all, I have to write script to make my usual job easy. Basically it involve reading a output of following pattern: crab: ExitCodes Summary >>>>>>>>> 45 Jobs with Wrapper Exit Code : 0 List of jobs:... (10 Replies)
Discussion started by: emily
10 Replies

4. Shell Programming and Scripting

[Solved] Assigning a value to a variable name then running a loop on these values

Hi, I was wondering if anyone could assist me for (what is probably) a very straightforward answer. I have input files containing something like File 1 Apples Apples Apples Apples File 2 Bananas Bananas Bananas Bananas (4 Replies)
Discussion started by: hubleo
4 Replies

5. UNIX for Advanced & Expert Users

couting occurences of a character inside a string and assigning it to a variable

echo "hello123" | tr -dc '' | wc -c using this command i can count the no of times a number from 0-9 occurs in the string "hello123" but how do i save this result inside a variable? if i do x= echo "hello123" | tr -dc '' | wc -c that does not work...plz suggest..thanks (3 Replies)
Discussion started by: arindamlive
3 Replies

6. Shell Programming and Scripting

Assigning value to script variable

I am trying to assign the value returned by wc command to a script variale. Code: FILES_NAME='files_list'; NO_OF_FILES =${wc -l $FILES_NAME}`; When the above code is run : it throws the error ${wc -l $FILES_NAME}: The specified substitution is not valid for this command. what is the... (6 Replies)
Discussion started by: hiten.r.chauhan
6 Replies

7. Shell Programming and Scripting

Problems assigning a string to a variable

Hello everyone, My problem looks quite simple , how to assign a string with spaces and lines "\n" to a variable. I've tried all kind of quoting and is impossible, bash always try to execute the string and never executes a simple assignation. This is part of the code ... (1 Reply)
Discussion started by: trutoman
1 Replies

8. Shell Programming and Scripting

Change existing variable value only user enters non-empty string.

I haven't checked any installation script to see how this is done.. But I could not even do following simple task. How do I Change existing variable value only when user enteres non-empty string. ? #!/usr/bin/ksh uid="scott" # Assign new value user enters to uid, else leave it... (7 Replies)
Discussion started by: kchinnam
7 Replies

9. Shell Programming and Scripting

ksh help assigning specific values to variable in script

Hi - Help needed. I have an input file that looks something like this, but with a lot more entries: A Customer1 B 4500 C 8000 A Customer2 B 6422 C 8922 I need to be able to print details for each customer on one line per customer. ie. if I could print these to a file and then cat... (3 Replies)
Discussion started by: frustrated1
3 Replies

10. Shell Programming and Scripting

Script stops running the remaining checks after becoming admin

Hi all, I encountered a problem where my script stops running the remaining checks after becoming an admin that is written within the script. For example: ========================================= #!/bin/sh check 1 # Runs successfully check 2 # Runs successfully /com/bin/admin #... (1 Reply)
Discussion started by: seanchew
1 Replies
Login or Register to Ask a Question