Sponsored Content
Top Forums Shell Programming and Scripting Awk variables not printing to StdOutput Post 302576540 by jjb1989 on Friday 25th of November 2011 07:04:00 AM
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

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
service(8)						      System Manager's Manual							service(8)

NAME
service - run a System V init script SYNOPSIS
service SCRIPT COMMAND [OPTIONS] service --status-all service --help | -h | --version DESCRIPTION
service runs a System V init script in as predictable an environment as possible, removing most environment variables and with the current working directory set to /. The SCRIPT parameter specifies a System V init script, located in /etc/init.d/SCRIPT. The supported values of COMMAND depend on the invoked script. service passes COMMAND and OPTIONS to the init script unmodified. All scripts should support at least the start and stop commands. As a special case, if COMMAND is --full-restart, the script is run twice, first with the stop command, then with the start com- mand. service --status-all runs all init scripts, in alphabetical order, with the status command. EXIT CODES
service calls the init script and returns the status returned by it. FILES
/etc/init.d The directory containing System V init scripts. ENVIRONMENT
LANG, TERM The only environment variables passed to the init scripts. SEE ALSO
/etc/init.d/skeleton, update-rc.d(8), init(8), invoke-rc.d(8). Jan 2006 service(8)
All times are GMT -4. The time now is 11:58 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy