如何用“ echo”打印命令的结果? [英] How do I print the result of a command with 'echo'?

查看:958
本文介绍了如何用“ echo”打印命令的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何打印以下全名?

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

每个例子,但 echo 命令,两侧带有一些单词:

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

例如,


你好Diogo Saraiva,很高兴再次见到你

Hello Diogo Saraiva, happy to see you again

Diogo Saraiva是我的全名Ubuntu记录。
我尝试了一些操作,但是我没有完成该脚本...

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

另一件事:为什么,当我发布 awk -F:'($ 1 == U){print $ 5}'U = $ LOGNAME / etc / passwd Diogo Saraiva ,,, 而不是 Diogo Saraiva

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

这种情况也会发生:

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

我需要我的脚本用命令声明一个变量 like,然后在脚本中多次回显该变量 like。

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

推荐答案

要输出所需的字符串,可以使用此命令。

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

如果 awk -F:-v U = $ LOGNAME'$ 1 == U {print $ 5}'/ etc / passwd 输出 Diogo Saraiva,,这就是该字段的 / 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"

这篇关于如何用“ echo”打印命令的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆