Problem with assigning output of grep + awk to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with assigning output of grep + awk to a variable
# 1  
Old 12-16-2008
Question Problem with assigning output of grep + awk to a variable

Hi All,

I am getting the output for the following command when i run it on the unix console.

---------------------------
grep `whoami` /etc/passwd | awk '{print ($1);}' | cut -d ":" -f3
----------------------------

But i made it into a script and tried to print the variable, its showing a syntax error.


tsts.sh
-------------------------------
#!/bin/sh
whoami_code=`(grep `whoami` /etc/passwd | awk '{print ($1);}' | cut -d ":" -f3)`
echo whoami_code=$whoami_code
-----------------------------------

the error is
tsts.sh: syntax error at line 1: `end of file' unexpected


Please help me out.

Thanks in advance..
# 2  
Old 12-16-2008
- Removed the backticks around the out brackets
- Added a dollar symobl in front of the first opening bracket
- removed the brackets around field 1 in the awk print and also the semicolon

Code:
whoami_code=$(grep `whoami` /etc/passwd | awk '{print $1}' | cut -d ":" -f3)

Should work.
# 3  
Old 12-16-2008
Data another error

i tried running that but got the following error.

tsts.sh: syntax error at line 3: `whoami_code=$' unexpected
# 4  
Old 12-16-2008
Code:
root@isau02:/data/tmp/testfeld> whoami_code=$(grep `whoami` /etc/passwd | awk '{print $1}' | cut -d ":" -f3)
root@isau02:/data/tmp/testfeld> echo $whoami_code
0 113 114 115

# 5  
Old 12-16-2008
Hi ,

Thankyou...

The error was coming because of #!/bin/sh in the beginning of the script.

-----------------------
#!/bin/sh
whoami_code=$(grep `whoami` /etc/passwd | awk '{print $1}' | cut -d ":" -f3)
echo $whoami_code
--------------------------
When i removed that part it worked... is there any particular reason for that??
# 6  
Old 12-16-2008
Idk, /bin/sh is bash. I tried both, /bin/sh and /bin/bash, both works; no idea.

Btw, use code tags when posting code, logs etc. Then you don't need the -------------.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep doesn't work when assigning to variable

Hello, First of all, I'd like to say hello to all members of forum. Can You please help me with the matter described below? I am trying to fetch a data from the file to variable, I am doing this using below script: returned=`tail -50 SapLogs.log | grep -i -E "Error|"` echo $returned ... (2 Replies)
Discussion started by: jedzio
2 Replies

2. UNIX for Dummies Questions & Answers

Tcsh command for assigning output of awk to variable

Hi I have a text file with 2 values and I am trying to assign each value to a variable and then write those to text files. So if the textfile is data.txt with 2 values x and y I want to assign mean=x, and stdev=y and then write these out in text files alongwith the id ($id has already been... (6 Replies)
Discussion started by: violin
6 Replies

3. Shell Programming and Scripting

Assigning bc output to a variable

I'm converting decimal to integer with bc, and I'd like to assign the integer output from bc to a variable 'val'. E.g. In the code below: If b is 5000.000, lines 6 and 8 will output: 5000 (5000.000+0.5)/1 | bc I'd like val to take the value 5000 though, rather than 5000.000 Does someone... (3 Replies)
Discussion started by: pina
3 Replies

4. Shell Programming and Scripting

Assigning output from awk to variable

I have a script whose contents are as below result= awk 's=100 END {print s }' echo "The result is" $result The desired output is The result is 100 My script is running without exiting and i am also not getting the desired output. Please help (5 Replies)
Discussion started by: bk_12345
5 Replies

5. Shell Programming and Scripting

Problem assigning cmd output to variable then using in IF statement

Hi, I'm using the bourn shell on a Sun Solaris Unix system. I am relatively new to UNIX scripting so please bear with me... I'm having a couple issues: 1) I need to have a variable $FSIZE set with the output of a command each time the script runs. (the command looks for a file and... (8 Replies)
Discussion started by: dqrgk0
8 Replies

6. Shell Programming and Scripting

problem in assigning substr to a variable inside awk

Hi All, I have a fixed-width datafile from which i need to extract value/string starting from some position to the specified length in each of the lines. awk '{print substr($0,x,y)}' datafile --- is working fine but awk 'BEGIN{a=0}{a=substr($0,x,y);print $a}' datafile ---is giving... (3 Replies)
Discussion started by: loggedin.ksh
3 Replies

7. UNIX for Dummies Questions & Answers

number of occurence using grep -c then assigning it to a variable

Hi guys! I need to count the occurence of a certain pattern. For example the pattern is PC. the contents of the file sample.txt: A PC asdfgadfjkl asdfa PC sadfaf fdsPCasdfg if i use grep -c PC sample.txt it will display 3 as the number of occurence how do i save that number to a... (1 Reply)
Discussion started by: khestoi
1 Replies

8. Shell Programming and Scripting

Assigning output of a command to variable

When I run time -p <command>, it outputs: real X.XX user X.XX sys X.XXwhere X.XX is seconds. How I can take just that first number output, the seconds of real time, and assign that to a variable? (9 Replies)
Discussion started by: jeriryan87
9 Replies

9. Shell Programming and Scripting

Assigning output to a variable

I am new to unix shell scripting. I was trying to convert each lines in a file to upper case. I know how to convert the whole file. But here i have to do line by line. I am getting it in the below mentioned script #!/bin/bash #converting lower to upper in a file #tr "" "" <file1... (3 Replies)
Discussion started by: jpmena
3 Replies

10. Shell Programming and Scripting

Assigning output of command to a variable

Hi, I'm trying to assign the output of a command to a variable and then concat it with another string, however, it keeps overwriting the original string instead of adding on to the end of the string. Contents of test.txt --> This is a test var1="`head -n 1 test.txt`" echo $var1 (This is a... (5 Replies)
Discussion started by: oma04
5 Replies
Login or Register to Ask a Question