Linux shell 脚本
您好,Stackoverflow 社区,
我在设计的 shell 脚本中遇到了一个问题...
#!/bin/sh
while :
do
clear
echo "-----------------------------
-----------"
echo "***************Main Menu****************"
echo "----------------------------------------"
echo "1. Backup Word Document"
echo "2. Backup Spreadsheet"
echo "3. Backup Picture"
echo "4. Restore Word Documents"
echo "5. Restore Spreadsheet"
echo "6. Restore Picture"
echo "7. EXIT"
echo "----------------------------------------"
pause
echo -n "Enter your menu choice [1-7]:"
read yourch
case $yourch in
1) echo ; tar -cvf /Files/*.doc /WP/ ; read ;;
1) echo "Today is"; date +%Y%m%d-%H:%M 2>&1 ; echo "Press a key..." ; read ;;
2) echo ; tar -cvf /Files/*.xls /EXCEL/ ; read ;;
2) echo "Today is"; date +%Y%m%d-%H:%M 2>&1 ; echo "Press a key..." ; read ;;
3) echo ; tar -cvf /Files/*.jpg /PICS/ ; read ;;
3) echo "Today is"; date +%Y%m%d-%H:%M 2>&1 ; echo "Press a key..." ; read ;;
4) echo ; tar xvzf /WP/*.doc ; read ;;
5) echo ; tar xvzf /EXCEL/*.xls ; read ;;
6) echo ; tar xvzf /PICS/*.jpg ; read ;;
7) exit 0 ;;
*) echo "Please press a number between 1 to 7";
esac
done
错误显示 'cript: line 21: 'cript: line 21: 'case $yourch 中出现意外标记附近的语法
错误知道如何绕过这个错误吗?基本上我想做的是能够备份一组文件名为“.doc”的文件并将它们放在备份文件夹中。然后我可以将文件从该文件夹恢复到另一个文件夹。
Hi Community of Stackoverflow,
I am facing a problem here with my shell script that I am designing...
#!/bin/sh
while :
do
clear
echo "-----------------------------
-----------"
echo "***************Main Menu****************"
echo "----------------------------------------"
echo "1. Backup Word Document"
echo "2. Backup Spreadsheet"
echo "3. Backup Picture"
echo "4. Restore Word Documents"
echo "5. Restore Spreadsheet"
echo "6. Restore Picture"
echo "7. EXIT"
echo "----------------------------------------"
pause
echo -n "Enter your menu choice [1-7]:"
read yourch
case $yourch in
1) echo ; tar -cvf /Files/*.doc /WP/ ; read ;;
1) echo "Today is"; date +%Y%m%d-%H:%M 2>&1 ; echo "Press a key..." ; read ;;
2) echo ; tar -cvf /Files/*.xls /EXCEL/ ; read ;;
2) echo "Today is"; date +%Y%m%d-%H:%M 2>&1 ; echo "Press a key..." ; read ;;
3) echo ; tar -cvf /Files/*.jpg /PICS/ ; read ;;
3) echo "Today is"; date +%Y%m%d-%H:%M 2>&1 ; echo "Press a key..." ; read ;;
4) echo ; tar xvzf /WP/*.doc ; read ;;
5) echo ; tar xvzf /EXCEL/*.xls ; read ;;
6) echo ; tar xvzf /PICS/*.jpg ; read ;;
7) exit 0 ;;
*) echo "Please press a number between 1 to 7";
esac
done
A error displays 'cript: line 21: syntax error near unexpected token 'in 'cript: line 21: 'case $yourch
Does anyone know how to by pass this error? Basically what im trying to do is be able to back up a set of files with the file name ".doc" and place them in a backup folder. I can then restore the files from this folder to another folder.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它在 RHEL 4 ES 上运行,使用 /bin/sh 作为解释器。但是,case 语句将仅执行第一个匹配项。因此,如果选项 1、2 和 3 中的每一个都需要执行两行,则只有第一行会被执行。因此,您可能想要的不是...
...您可能想要更多类似这样的内容:
根据您的解释器,您可能还会遇到
*)
案例不以;;< 结尾的问题/代码>。
然而,第一个建议是调试一个不同的问题,第二个建议的可能性不大。如果您在 Windows 中编辑了此内容并复制到粘贴缓冲区中,则第 21 行中也可能出现错误的行结束符(可能还有更多信息)。解决方法是针对脚本运行 dos2unix 命令 - 即 dos2unix /path/to/script.sh。
This runs for me on RHEL 4 ES using /bin/sh as the interpreter. However, the case statement will execute only the first match. So where you have two lines to execute for each of options 1, 2 and 3, only the first one is ever executed. So instead of...
...you may want something more like this:
Depending on your interpreter, you may also have a problem that your
*)
case does not end in;;
.However, the first of these suggestions is debugging a different problem and the second is a long shot. If you edited this in Windows and copied over in a paste buffer, you may also have an incorrect line-end in Line 21 (and possibly more below). The fix for that is to run the
dos2unix
command against the script - i.e.dos2unix /path/to/script.sh
.