这是一个 .sh 文件,Ubuntu 终端运行该文件后立即退出。我的脚本有什么问题吗?
我不知道我的问题是什么 - 为什么终端在运行脚本后关闭并且不让我看到结果。该文件显示在此处。在终端中输入每个命令都可以工作,但是将它们放在文件 .sh 中,就会出现如上所述的问题。
echo
echo $PATH
echo
nslookup www.fiu.edu
echo
netstate-a
echo
traceroute www.google.com
echo
ifconfig
答案1
我不是 100% 确定你怎么样跑步这个脚本,但我会像这样重写(我还添加了注释):
#!/bin/sh
# This is a comment line, but the line above must be the first line
# and defines the shell that the rest of this script will use.
echo "PATH: ["$PATH"]"
# Personally, I like to include [] around vars so I can see
# exactly what they are
# Run a few network related commands
nslookup www.fiu.edu
netstate -a
traceroute www.google.com
ifconfig
# Pause the script with a question. This will stop the script
# from simply closing. Default to "y" as well so the user can
# just hit the enter key to exit.
echo -n "Finished? [y/n](y) "
read ans
# Check what the user typed - if anything
if [ "$ans" = "" -o "$ans" = "Y" -o "$ans" = "y" ]
then
# Exit with 0 to signify no issue.
exit 0
else
echo "All done, so exiting anyway :]"
exit 0
fi