Awk variables not printing to StdOutput


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk variables not printing to StdOutput
# 1  
Old 11-25-2011
Awk variables not printing to StdOutput

Hi guys, I'd be grateful if you could have a look at my code below and give me some feedback as to why the homedir and fullname variables aren't being printed to Standard Output when the script is run. Many thanks.

Code:
read -p "Please type in username you wish to get information from: " username

username="$1"

if grep "$username" information_file
then
   # use awk with -v option to pass in shell variable username to awk. Check if first field $1 is equal to username
   fullname=$(awk -v username="$username" 'BEGIN{FS=":"} $1==username {print $5}' /etc/passwd)

   homedir=$(awk -v username="$username" 'BEGIN{FS=":"} $1==username{print $6}' /etc/passwd)

   # pass the variables to awk. "found" is a toggle flag to signify if user is found. If it is 0, means not found
   awk -v username="$username"  -v homedir="$homedir" -v fullname="$fullname" 'BEGIN{FS=":"; found=0}$1==username
   {
        print "Full name: " fullname
        print "Address: " $2
        print "E-mail address: " $3
        print "Home Directory: " homedir
        print "Manager name: " $5
        found=1
   }' information_file


else
  echo "$username not found in Information File. Exiting.."
  exit 0
fi

# 2  
Old 11-25-2011
What output are you getting?
# 3  
Old 11-25-2011
Quote:
Originally Posted by CarloM
What output are you getting?
None at all CarloM. Just for example "Fullname: ".

Variable values aren't being displayed.

Thanks.
# 4  
Old 11-25-2011
Any variable values? Just paste some of the output (and preferably some of the corresponding input).
# 5  
Old 11-25-2011
Assuming your information_file is /etc/passwd... Try this...
Code:
#!/bin/bash

read -p "Please type in username you wish to get information from: " username

#username="$1"

if grep "^$username" information_file >/dev/null
then
   # use awk with -v option to pass in shell variable username to awk. Check if first field $1 is equal to username
   fullname=$(awk -v username="$username" 'BEGIN{FS=":"} $1==username {print $5}' /etc/passwd)

   homedir=$(awk -v username="$username" 'BEGIN{FS=":"} $1==username{print $6}' /etc/passwd)

   # pass the variables to awk. "found" is a toggle flag to signify if user is found. If it is 0, means not found
   awk -v username="$username"  -v homedir="$homedir" -v fullname="$fullname" 'BEGIN{FS=":"; found=0}
   $1==username && found==0{
        print "Full name: " fullname
        print "Address: " $2
        print "E-mail address: " $3
        print "Home Directory: " homedir
        print "Manager name: " $5
        found=1
   }' information_file
else
  echo "$username not found in Information File. Exiting.."
  exit 0
fi

--ahamed
# 6  
Old 11-25-2011
hint
Why are you reading username when you are overiding that later with $1


advice
along with the question, put how did you execute the command, what output did you get like
./yourscript rakesh
./youscript
and the output
secondly, its a good practice to use the variable like this ${user}, becuase $u $us $use
can also have values, which will make it hard to judge the cause of problem

third point is not much important shall make mainitining and debuging the script easier, try to make more functions and call functions,
for example

main(){
user=$1;
infoFile="information_file";
getFullName ${user};
getFullPath ${user};
getUserDetails ${user} ${infoFile};
}

main $*

This would have benfit of the following
->You cannot make mistake of writing the filename incorrectly at differnt place
->debug if the problem was in getting username or path or whatever
->later if you need to perform similar funcitons, copy paste it or source it, provided
there is no main
->if you want to change the way the full path is derived, just modify the specific functions, and can be confident that rest is working as usual, very useful when working with files and patterns(where the field might change).
->and last but not the least, its looks beautiful(like other languages) and easily get intended when working with vim or other editor.
->further it gives more understanding of bash, and also you could exploit full bash feature like any other languages.

i sometimes deal with script more than 100 lines, and it becomes easier and i have less need to remember things, when i look back after many months.Its a boring in the beginning, but once you start, you cant resist writing like that.
This User Gave Thanks to uvrakesh For This Post:
# 7  
Old 11-25-2011
Thanks everyone. The problem was fixed. Uvrakesh, your hint made all the difference thanks. I removed the overriding username =$1 and everything now works normal. Thanks agaiN!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Echo not printing the variables with delimiters as required

This is the file which contains only neccessary values from the output of curl command i.e TEMP_FILE Proxy Hostname server0123.domain.com Proxy IP address XXX.XXX.XX.XX port 0000 Proxy Version SGOS X.X.X.X Proxy Serial # ... (5 Replies)
Discussion started by: ramprabhum
5 Replies

2. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

3. Shell Programming and Scripting

awk - Why can't value of awk variables be passed to external functions ?

I wrote a very simple script to understand how to call user-defined functions from within awk after reading this post. function my_func_local { echo "In func $1" } export -f my_func_local echo $1 | awk -F"/" '{for (k=1;k<=NF;k++) { if ($k == "a" ) { system("my_local_func $k") } else{... (19 Replies)
Discussion started by: sreyan32
19 Replies

4. Shell Programming and Scripting

ksh passing to awk multiple dyanamic variables awk -v

Using ksh to call a function which has awk script embedded. It parses a long two element list file, filled with text numbers (I want column 2, beginning no sooner than line 45, that's the only known thing) . It's unknown where to start or end the data collection, dynamic variables will be used. ... (1 Reply)
Discussion started by: highnthemnts
1 Replies

5. Shell Programming and Scripting

awk question for printing variables

Hi All, I have the following awk code where I am passing 4 variables to the program and I need to print them in the awk script. The variables are $start_month $start_date - $end_month $end_date. printf("\tFor More Information\n") > out_tmp1 printf("\tIf you have any questions about this... (6 Replies)
Discussion started by: nua7
6 Replies

6. Shell Programming and Scripting

Printing using awk

Hi I am relatively new to awk so i am getting confused a lot I am in need of help ... I am trying to append coloumns to the end of line using AWK I tried using this command awk -F "," '{for(s=7;s<=217;s++);$s="0";}1' OFS=, sam_sri_out It is giving me an output like this...... (1 Reply)
Discussion started by: Sri3001
1 Replies

7. Shell Programming and Scripting

Help with awk: printing variables

awk is only printing out the variables on the first AK2, how do I get it to print out the variables for every occurrence of AK2? grep -l "^ST.997" *.replaced|sort -n|uniq>FILELIST while read file do let "count = $count+1" <$file awk 'BEGIN { FS="" ... (0 Replies)
Discussion started by: verge
0 Replies

8. Shell Programming and Scripting

AWK printing

i have a file containing a line 123456 is it possible to use AWK to print it out to look like 1 2 3 4 5 6 (8 Replies)
Discussion started by: tomjones
8 Replies

9. Shell Programming and Scripting

AWK Printing

i have a file and i want to print the second variable and add qoutes to it i do awk -F"|" '{print $2}' star.unl. i get the output xxxxxxx but i need the variable($2) to be in quotes.like "xxxxxxx" how do i do there please (3 Replies)
Discussion started by: tomjones
3 Replies

10. Shell Programming and Scripting

AWK printing

Hello, I am trying to write a formatted report into a file using .ksh script and awk. Here is the command I am trying to run echo "before awk" ${SRC_SCHEMA} echo | awk '{printf "%-20s", ${SRC_SCHEMA} }' >>$REPORT_SQL_NAME I get the following error before awk ADW awk: 0602-562 Field $()... (1 Reply)
Discussion started by: fastgoon
1 Replies
Login or Register to Ask a Question