看看代码:
#!/bin/bash
read -p "Eneter 1 for UID and 2 for LOGNAME" choice
if [ $choice -eq 1 ]
then
read -p "Enter UID: " uid
logname=`cat /etc/passwd | grep $uid | cut -f1 -d:`
else
read -p "Enter Logname: " logname
fi
not=`ps -au$logname | grep -c bash`
echo "The number of terminals opened by $logname are $not"
Run Code Online (Sandbox Code Playgroud)
此代码用于查找用户在同一台 PC 上打开的终端数量。现在有两个用户登录,比如 x 和 y。我目前以 y 身份登录,用户 x 中打开了 3 个终端。如果我使用上面提到的不同方式在 y 中执行此代码,结果是:
$ ./file.sh
The number of terminals opened …
Run Code Online (Sandbox Code Playgroud) 我有以下简单的脚本:
#!/bin/bash
echo "Bash version ${BASH_VERSION}..."
for i in {1..99..2}
do
echo $i
done
Run Code Online (Sandbox Code Playgroud)
如果我运行,输出sh file.sh
:
Bash version ...
{1..99..2}
Run Code Online (Sandbox Code Playgroud)
如果我运行,输出bash file.sh
:
Bash version 4.2.25(1)-release...
1
3
5
.
.
.
99
Run Code Online (Sandbox Code Playgroud)
我有两个疑问:
如果我写了一个shabang行指定的外壳,不应该将其与运行bash的我无论是使用sh file.sh
还是bash file.sh
?
我知道$BASH_VERSION
sh 无法识别,但是 for 循环有什么问题?为什么不打印数字?