wish逐行执行shell脚本
时间: 2023-12-16 16:28:14 浏览: 39
为了逐行执行shell脚本,可以使用以下命令:
```shell
bash -x script.sh
```
其中,`script.sh`是你要执行的脚本文件名。使用`-x`选项可以打印出每一行执行的命令和结果,方便调试和排除错误。
另外,如果你想在脚本中逐行执行某一部分代码,可以使用`set -x`和`set +x`命令来打开和关闭调试模式。例如:
```shell
#!/bin/bash
set -x
echo "This is a debug message"
set +x
echo "This is a normal message"
```
在这个例子中,`set -x`命令会打开调试模式,执行`echo`命令时会输出调试信息。`set +x`命令会关闭调试模式,后面的`echo`命令不会输出调试信息。
相关问题
、编写一个menu的shell脚本程序,有如下几行 Number Name For Linux Menu 1 exit leave menu or return 2 menu goto another local menu 3 vi deit a file 4 mail read a mai l 5 send send mail to someone 6 cal see your calendar . 7 who see who is on the system 8 ls list the fi les in this directory 9 cat display a file on the screen Please enter a number or a name for the action you wish: 要求具备清屏功能
好的,这是一个简单的menu shell脚本程序,具备清屏功能:
```
#!/bin/bash
while true
do
clear
echo "For Linux Menu"
echo "1 exit leave menu or return"
echo "2 menu goto another local menu"
echo "3 vi deit a file"
echo "4 mail read a mail"
echo "5 send send mail to someone"
echo "6 cal see your calendar"
echo "7 who see who is on the system"
echo "8 ls list the files in this directory"
echo "9 cat display a file on the screen"
read -p "Please enter a number or a name for the action you wish: " choice
case $choice in
1 | exit)
break
;;
2 | menu)
echo "Going to another local menu..."
sleep 1
;;
3 | vi)
read -p "Please enter the file name you wish to edit: " filename
vi $filename
;;
4 | mail)
echo "Reading a mail..."
sleep 1
;;
5 | send)
read -p "Please enter the recipient's email address: " recipient
read -p "Please enter the subject of the email: " subject
read -p "Please enter the message you wish to send: " message
echo "Sending mail to $recipient with subject '$subject' and message '$message'..."
sleep 1
;;
6 | cal)
cal
;;
7 | who)
who
;;
8 | ls)
ls
;;
9 | cat)
read -p "Please enter the file name you wish to display: " filename
cat $filename
;;
*)
echo "Invalid choice. Please try again."
sleep 1
;;
esac
done
```
使用方法:
1. 将上述脚本粘贴到一个文件中,例如menu.sh。
2. 给该文件执行权限:`chmod +x menu.sh`。
3. 在终端中进入该文件所在目录,执行:`./menu.sh`。
4. 按照提示输入对应的数字或名称执行相应的操作,输入1或exit退出菜单。
编写一个menu的shell脚本程序,执行后的界面为: Number Name For Linux Menu 1 exit leave menu or return 2 menu goto another local menu 3 vi deit a file 4 mail read a mai l 5 send send mail to someone 6 cal see your calendar . 7 who see who is on the system 8 ls list the fi les in this directory 9 cat display a file on the screen Please enter a number or a name for the action you wish: 要求有清屏功能,能正确地执行各项命令并显示。
好的,以下是一个简单的实现:
```bash
#!/bin/bash
# 定义菜单选项数组
options=("exit" "menu" "vi" "mail" "send" "cal" "who" "ls" "cat")
# 定义菜单选项名称数组
names=("leave menu" "goto another local menu" "deit a file" "read a mail" "send mail to someone" "see your calendar" "see who is on the system" "list the files in this directory" "display a file on the screen")
# 定义清屏函数
clear_screen() {
if [[ "$OSTYPE" == "darwin"* ]]; then
clear
else
reset
fi
}
# 显示菜单
show_menu() {
clear_screen
echo "Number Name"
echo "For Linux Menu"
for i in ${!options[@]}; do
echo "$i ${names[$i]}"
done
echo ""
}
# 执行命令
execute_command() {
if [[ "$1" =~ ^[0-9]+$ ]]; then
# 根据数字执行对应的命令
if [[ "$1" -ge 0 && "$1" -lt ${#options[@]} ]]; then
if [[ "$1" -eq 0 ]]; then
# 退出脚本
exit 0
elif [[ "$1" -eq 1 ]]; then
# 进入另一个菜单,这里简单地调用show_menu函数
show_menu
else
# 执行命令
command=${options[$1]}
echo ""
read -p "Please enter the $command: " args
eval "$command $args"
fi
else
# 数字不在菜单选项范围内
echo "Invalid option: $1"
fi
else
# 根据名称执行对应的命令
index=-1
for i in ${!names[@]}; do
if [[ "${names[$i]}" == "$1" ]]; then
index=$i
break
fi
done
if [[ "$index" -ne -1 ]]; then
execute_command $index
else
# 名称不在菜单选项范围内
echo "Invalid option: $1"
fi
fi
}
# 主程序
show_menu
while true; do
read -p "Please enter a number or a name for the action you wish: " option
execute_command $option
read -p "Press ENTER to return to menu..." enter_key
show_menu
done
```
这个脚本定义了两个数组,一个是菜单选项数组,包含了9个命令,另一个是菜单选项名称数组,包含了对应的名称。在show_menu函数中,遍历这两个数组输出菜单。在execute_command函数中,根据输入的数字或名称执行对应的命令。最后,在主程序中循环读取输入,执行命令,并显示菜单。