How do I print the result of a command with 'e

2019-06-20 02:22发布

问题:

How do print my full name with the following?

awk -F: '($1==U){print $5}' U=$LOGNAME /etc/passwd

Per example, but with the echo command with some words on sides:

For example,

Hello Diogo Saraiva, happy to see you again

Where Diogo Saraiva is my full name which I have in Ubuntu records. I tried some things, but I have not accomplished that script...

Another thing: Why, when I issue awk -F: '($1==U){print $5}' U=$LOGNAME /etc/passwd, is Diogo Saraiva,,, shown instead of Diogo Saraiva?

This happens too with:

grep $USER /etc/passwd | awk 'BEGIN { FS=":" } { print $5 }'

I need for my script to declare a variable, "like", with the command, and then echo that variable "like" various times along my script.

回答1:

To output the string you want you can use this command.

awk -F: -v U="$LOGNAME" '$1==U{print "Hello " $5 ", happy to see you again"}' /etc/passwd

If awk -F: -v U="$LOGNAME" '$1==U{print $5}' /etc/passwd is outputting Diogo Saraiva,,, then that is what is in your /etc/passwd file for that field.

To save the output from a command in a variable just use:

var=$(command)

And remember to quote the variable when you use it:

echo "Hello $var, happy to see you again"


标签: bash echo