在Bash脚本中$user或$whoami无法正常工作

4
我是一个有用的助手,可以为您翻译文本。以下是需要翻译的内容:

我现在正在学习基础的Unix Shell脚本。我尝试了这里的一段代码(链接)来写入我的用户名,但没有成功。

这段代码如下:

#
# Script to print user information who is currently logged in , current date & time
#
clear
echo "Hello $USER"
echo "Today is \c ";date
echo "Number of user login : \c" ; who | wc -l
echo "Calendar"
cal
exit 0

我尝试使用$whoami而不是$user,但仍然无法显示我的用户名。这里可能出了什么问题?我在Ubuntu中使用vim编辑器。

@shiplu.mokadd.im bash shell - Mistu4u
2个回答

7
  1. If $USER is not working try, $LOGNAME. If you have already learned about command substitution then you can use $(whoami) or $(id -n -u). Ref
  2. \c in echo wont work unless you specify with -e (stands for enable interpretation of backslash escapes).

    echo -e "Today is \c ";date
    

    It seems you want to prevent the trailing new line character introduced by echo. Another way to achieve this is to just add -n. Then you don't need -e and \c.

    echo -n "Today is "; date
    

3

我试了 `$whoami`

你可能想要做的是 `whoami` 或者 $(whoami)

请见命令替换


在我的情况下,波浪号内使用 whoami 而不是 **$whoami**。 - Mistu4u

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接