2013-11-20 20 views
0

无法真正找到与此类似的线程,所以我想我会创建一个新线程。我试图制作一个基本的bash脚本,一次显示当前目录中的文件名称。简单的脚本错误1)在case语句中的文件列表脚本

一个echo声明说'你想预览这个文件吗?是/否“,如果是,则代码继续使用头语法显示该文件的前三行,然后程序退出。

所以这是我在脑子里,到目前为止,

#!/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 

程序在第23行显示错误“1)”刚开始的情况下,后声明。这不正确的语法?我不太清楚为什么它会这样做。

回答

1

首先,你错过了in

case "$boolean" in 
    1) 

其次,你错过了;;

read boolean ;; 

而且,引用您的变量。它似乎也有其他错误。

+0

特别是,'if ...;然后 ...;其他...; fi'语法在一些情节中被严重破坏...... – twalberg