12-16-2008
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..
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
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
2. Shell Programming and Scripting
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
3. Shell Programming and Scripting
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
4. UNIX for Dummies Questions & Answers
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
5. Shell Programming and Scripting
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
6. Shell Programming and Scripting
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
7. Shell Programming and Scripting
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
8. Shell Programming and Scripting
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
9. UNIX for Dummies Questions & Answers
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
10. Shell Programming and Scripting
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