我的电脑中的 Bash 脚本错误,但在其他一些电脑上没有

Hai*_* M. 3 bash

剧本:

waktu=$(date +"%H")
kelompok="E20"
dir_skrg=$(pwd)

if (( $waktu >= 5 && $waktu <= 10 ))
then
    salam="pagi"
elif (( $waktu >= 10 && $waktu <= 3 ))
then
    salam="siang"
elif (( $waktu >= 4 && $waktu <= 7 ))
then
    salam="sore"
else 
    salam="malam"
fi

echo “Selamat $salam $kelompok dengan user $USER, sekarang pukul $waktu dan pada direktori $dir_skrg”
Run Code Online (Sandbox Code Playgroud)

它给出了错误:

script1.sh: 5: script1.sh: 14: not found
script1.sh: 8: script1.sh: 14: not found
script1.sh: 11: script1.sh: 14: not found
Run Code Online (Sandbox Code Playgroud)

但不是在我朋友 ubuntu 的。有人知道为什么吗?

14 是我运行脚本的时间,我假设出于某种原因它认为 14 是一个文件

dev*_*av2 5

添加#!/bin/bash到脚本的顶部,作为第一行。

注意:这称为shebang。可以在此处找到有关它的更多信息。

为您的脚本提供正确的权限。

chmod a+x <script_name>
Run Code Online (Sandbox Code Playgroud)

再次运行您的脚本。

./<script_name>
Run Code Online (Sandbox Code Playgroud)


Jan*_*ary 5

看起来它是作为 shell (sh) 脚本而不是 bash 脚本运行的。尝试像这样运行它:

bash script.sh
Run Code Online (Sandbox Code Playgroud)

或输入以下内容作为脚本的第一行

#!/bin/bash
Run Code Online (Sandbox Code Playgroud)

然后运行为 ./script.sh