Bash シェル スクリプト エラー:./myDemo:56:構文エラー:終了していない引用符で囲まれた文字列

StackOverflow https://stackoverflow.com/questions/1861285

  •  13-09-2019
  •  | 
  •  

質問

誰かがこのコードを見て、何が問題なのかを見つけてもらえませんか?

#!/bin/sh
while :
do
    echo " Select one of the following options:"
    echo " d or D) Display today's date and time"
    echo " l or L) List the contents of the present working directory"
    echo " w or W) See who is logged in"
    echo " p or P) Print the present working directory"
    echo " a or A) List the contents of a specified directory"
    echo " b or B) Create a backup copy of an ordinary file"
    echo " q or Q) Quit this program"
    echo " Enter your option and hit <Enter>: \c"
    read option 
    case "$option" in
        d|D) date
             ;;
        l|L) ls $PWD
             ;;
        w|w) who
                 ;;
        p|P) pwd
             ;;
        a|A) echo "Please specify the directory and hit <Enter>: \c"
             read directory
                    if [ "$directory = "q" -o "Q" ]
                then
                    exit 0
                fi

                while [ ! -d "$directory" ]
                do
                        echo "Usage: "$directory" must be a directory."
                    echo "Re-enter the directory and hit <Enter>: \c"
                    read directory

                        if [ "$directory" = "q" -o "Q" ]
                        then    
                            exit 0

                        fi

                done
                    printf ls "$directory"

            ;;  
            b|B) echo "Please specify the ordinary file for backup and hit <Enter>: \c"
             read file
                if [ "$file" = "q" -o "Q" ]
                then
                    exit 0
                fi     

                while [ ! -f "$file" ]
                do
                    echo "Usage: \"$file\" must be an ordinary file."
                    echo "Re-enter the ordinary file for backup and hit <Enter>: \c"
                    read file
                        if [ "$file" = "q" -o "Q" ]
                        then
                            exit 0
                        fi              
                done
                    cp "$file" "$file.bkup"
                 ;;

        q|Q) exit 0
             ;;

    esac
    echo
done
exit 0

理解できない構文エラーがいくつかあります。ただし、この Unix システムでは echo -e は機能しないことに注意してください (なぜ私が知らないのか、変更するためのいかなる権限も持っていないのかは聞かないでください。たとえ許可されなかったとしても)に)

Bash シェル スクリプト エラー:"./myDemo ./myDemo:62行目:予期しないトークン付近で構文エラーが発生しました done' ./myDemo: line 62:「[編集済み]

編集: while文のエラーを修正しましたが、 スクリプト まだいくつかはそうではありません 正しく動作しています。

  1. b|B) switch ステートメントでは

    cp $file $file.bkup 実際にファイルをfile.bkupにコピーしませんか?

  2. a|A) switch ステートメント内

ls "$ディレクトリ" ユーザーが表示できるディレクトリリストを出力しません ?

#!/bin/bash
while $TRUE
do
        echo " Select one of the following options:"
        echo " d or D) Display today's date and time"
        echo " l or L) List the contents of the present working directory"
        echo " w or W) See who is logged in"
        echo " p or P) Print the present working directory"
        echo " a or A) List the contents of a specified directory"
        echo " b or B) Create a backup copy of an ordinary file"
        echo " q or Q) Quit this program"
        echo " Enter your option and hit <Enter>: \c"
        read option
        case "$option" in
                d|D) date
                     ;;
                l|L) ls pwd
                     ;;
                w|w) who
                     ;;
                p|P) pwd
                     ;;
                a|A) echo "Please specify the directory and hit <Enter>: \c"
                     read directory
                        if [ ! -d "$directory"  ]
                        then
                                while [ ! -d "$directory" ]
                                do
                                        echo "Usage: "$directory" must be a directory."
                                        echo "Specify the directory and hit <Enter>: \c"
                                        read directory

                                        if [ "$directory" = "q" -o "Q" ]
                                        then
                                        exit 0

                                        elif [ -d "$directory" ]
                                        then
                                                ls "$directory"

                                        else
                                        continue
                                        fi
                                done
                        fi
                        ;;
                b|B) echo "Specify the ordinary file for backup and hit <Enter>: \c"
                     read file
                        if [ ! -f "$file" ]
                         then
                                while [ ! -f "$file" ]
                                do 
                                        echo "Usage: "$file" must be an ordinary file."
                                        echo "Specify the ordinary file for backup and hit <Enter>: \c"
                                        read file
                                        if [ "$file" = "q" -o "Q" ]
then
                                        exit 0
                                        elif [ -f "$file" ]
                                        then
                                        cp $file $file.bkup
                                        fi
                                done
                        fi
                        ;;

                q|Q) exit 0
                     ;;

        esac
        echo
done
exit 0

別物...コードを自動解析するために使用できるエディタはありますか?つまり、NetBeans に似たものでしょうか?

役に立ちましたか?

解決

あなたには次のものが欠けています do 2回目以降 while. 。(「B」の場合。これを上記の「A」のケースと比較してください。)

私はシェルスクリプトを構文強調表示する gvim を使用していますが、エディタについては別の質問として尋ねる必要があると思います。


変更された質問については、次のようになります。あなたの論理は両方とも壊れています A そして B ケース:if/while ネストからバックアップ ロジックを引き出す必要があります...の if 実際にはあなたのために何もしていません。また、スペースによってスクリプトが中断されないように、すべてのファイル名を必ず引用符で囲んでください。ネストされた引用符をエスケープします。が必要だと思います -e を使用する echo ステートメントについて \c.

したがって、次のようなことを行います。

b|B) echo -e "Specify the ordinary file for backup and hit <Enter>: \c"
    read file
    while [ ! -f "$file" ]
    do 
        echo "Usage: \"$file\" must be an ordinary file."
        echo -e "Specify the ordinary file for backup and hit <Enter>: \c"
        read file
        if [ "$file" = "q" -o "$file" = "Q" ]
        then
            exit 0
        fi
    done
    cp "$file" "$file.bkup"
    ;;

に対しても同様の変更を行う必要があります。 A 場合。

他のヒント

A) ディレクトリ一覧セクションの 2 つの問題。

  1. -o 接続詞は思ったように機能しません。こうあるべきです:

                if [ "$directory" = "q" -o "$directory" = "Q" ]
    
  2. 外側の「if」には、指定されたディレクトリが実際に最初からディレクトリである場合を処理するために「else」が必要です。

B) バックアップセクションにも同じ 2 つの問題があります。これらを修正すると、両方のコマンド オプションが機能するようになります。

あなたが引用したのは、 $file ほとんどの場所で変化しますが、 cp 命令しないでください。そのはず:

cp "$file" "$file.bkup"

あなたの一部 echo コマンドの最後に「\c」が付きます。それは csh 特有のものだと思います。Bash は文字「\」と「c」を文字通りエコーするだけです。

あなたの声明 while $TRUE 変数が null または設定されていないことによって機能します。何らかの値が設定されると、その内容をコマンドとして実行しようとします。このタイプの無限ループを実行したい場合は、通常、次のように Bash で実行されます。

while true

ここで、 true はシェル組み込みです。または:

while :

ここで、コロンは true を返す no-op です。もちろん、同じことを達成する他の方法もあります。

の中に l|L) おそらく次のいずれかを実行したい場合:

ls

または

ls $PWD

今のやり方では、「pwd」という名前のファイルのエントリをリストしようとします。

vim と nano はどちらも Bash の構文ハイライトを実行できます。まだセットアップされていない場合は、 ~/.nanorc そして ~/.vimrc これらを行うことができます:

ナノの場合:

nano -Y sh scriptname

vim の場合:

:syntax on
:set filetype=sh

1) あまり多くのエコーを使用しないでください。メニュー システムを作成するには、cat here-document を使用できます。例:

cat <<EOF
-------------------------------------------------------
Select one of the following options:
d or D) Display today's date and time
l or L) List the contents of the present working directory
w or W) See who is logged in
p or P) Print the present working directory
a or A) List the contents of a specified directory
b or B) Create a backup copy of an ordinary file
q or Q) Quit this program
--------------------------------------------------------
EOF

あるいはこれでも大丈夫

echo "-------------------------------------------------------
Select one of the following options:
d or D) Display today's date and time
l or L) List the contents of the present working directory
w or W) See who is logged in
p or P) Print the present working directory
a or A) List the contents of a specified directory
b or B) Create a backup copy of an ordinary file
q or Q) Quit this program
--------------------------------------------------------"

2) use にオプションの選択を求める場合、-p を付けて read を使用できます。例:

read -p "Enter your option and hit <Enter>: " choice

3) printf は echo より移植性が高いため、可能な限り printf を使用する必要があります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top