该脚本在 Mac 机器上执行并创建输出文件,但在 Ubuntu 机器上它会生成错误消息。Bash shell 用于两个实例。:
1 - /var
2 - /etc :
1
: bad variable name: read: word
first_part(1).sh: 6: first_part(1).sh: Syntax error: newline unexpected (expecting ")")
Run Code Online (Sandbox Code Playgroud)
——
echo "To scan through the directories /var and /etc type 1 or 2: "
echo "1 - /var"
echo "2 - /etc : "
read word
case $word in
1)
find /var -type d -follow -ls | awk '{print $3, $5, $6, $11}' > var.txt
echo "Your file containing /var information has been created."
;;
2)
find /etc -type d -follow -ls | awk '{print $3, $5, $6, $11}' > etc.txt
echo "Your file containing /etc information has been created."
;;
*)
echo "Please insert a valid input"
continue
;;
esac
Run Code Online (Sandbox Code Playgroud)
如果您使用 执行该文件sh filename.sh
,那么一个问题是,在您的 Ubuntu 系统上,这可能不会执行,bash
而是其他一些 shell。在我的 Ubuntu 12.04 系统sh
上/bin/sh
是软链接/bin/dash
(使用d
; 参见“Dash as /bin/sh”)。
您应该使用bash filename.sh
, 或使用 shebang 行并使文件可执行 ( chmod +x filename.sh
)。
#!/bin/bash
echo "To scan through the directories /var and /etc type 1 or 2: "
echo "1 - /var"
.
.
Run Code Online (Sandbox Code Playgroud)
将文件从 Mac 移动到 Ubuntu 时要检查的一件事是文件的换行符(使用od -c file_name
),如果输出中有 '\r' 字符,但\n
您不必进行转换,例如使用:
tr '\r' '\n' < file_name > new_file_name
.