我真的找不到一个和这个类似的线程,所以我想我应该创建一个新的线程。我正在尝试创建一个基本的bash脚本,该脚本一次显示当前目录中一个文件的名称。
一个echo语句说“你想预览这个文件吗?是/否”,如果是,那么代码继续使用head语法显示该文件的前3行,然后程序退出。
到目前为止,这就是我的想法,
#!/bin/bash
for file in ./* #This points to current directory with ./* and views files inside.
do
if [[ -f $file ]] #If File Exists, read it
read file
then
echo -e 'Would you like to view this file? Press 1 For Yes and 2 For No'
read boolean #reads the prompt 1 or 2 to then proceed with the case statement below
case $boolean
1)
echo -e 'Preview of File...'
read boolean
2)
echo -e 'Exit Program...'
read boolean
if [ boolean -eq 1 ] #This part of the code finds out if the prompt was answered with 1 (yes), if it was then it produces the first 3 lines of the file, if it was 2 then it quits the program.
then line=$(head -n 3 file)
else
*( echo -e 'Please enter a valid option' #If someone enters 3-9 then the program will require them to enter a valid number.
esac
done
fi在启动case语句之后,程序在第23行"1)“处显示错误。这不是正确的语法吗?我不太确定为什么它会这么做。
发布于 2013-11-21 02:13:16
首先,你错过了in
case "$boolean" in
1)第二,你错过了;;
read boolean ;;另外,引用你的变量。似乎还有其他错误。
https://stackoverflow.com/questions/20103629
复制相似问题