相关疑难解决方法(0)

使用“source file.sh”、“./file.sh”、“sh file.sh”、“../file.sh”执行shell脚本有什么区别?

看看代码:

#!/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)

command-line bash scripts

14
推荐指数
1
解决办法
7942
查看次数

为什么在 sh 和 bash 的情况下我会得到不同的输出?

我有以下简单的脚本:

#!/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)

我有两个疑问:

  1. 如果我写了一个shabang行指定的外壳,不应该将其与运行bash的我无论是使用sh file.sh还是bash file.sh

  2. 我知道$BASH_VERSIONsh 无法识别,但是 for 循环有什么问题?为什么不打印数字?

command-line bash

8
推荐指数
2
解决办法
2116
查看次数

标签 统计

bash ×2

command-line ×2

scripts ×1