GNU/Linux >> Linux の 問題 >  >> Linux

入力が「はい」の場合、スクリプトを再実行しますか?

簡単なファイル処理スクリプトをまとめています。最後を除いてすべてうまく機能しています。do you want to perform another actionと言ってください。 答えがyesの場合 次に、スクリプトを再開します。ここにある種のループが必要なことはわかっていますか?これが私が持っているものです:

#/bin/bash


echo "Select an option from copy , remove , rename , linking"
#read in user input into the action variable

read action

# if action is copy then continue proceed with the following
if [ $action = "copy" ]
then
echo "Please enter the filename you wish to copy"
read filename
# check if filename exists, if it doesn't then exit program
    if [ ! -e  $filename ] 
    then
    echo "$filename does not exist"
    exit 1
    fi
echo "Please enter the new filename"
read filenamenew
cp $filename $filenamenew
echo "$filename has been copied to $filenamenew"

# if action is remove then continue proceed with the following
elif [ $action = "remove" ]
then
echo "Please enter the filename you wish to remove"
read filename
# check if filename exists, if it doesn't then exit program
    if [ ! -e  $filename ] 
    then
    echo "$filename does not exist"
    exit 1
    fi
rm -rf $filename
echo "$filename has been deleted"

# if action is rename then continue proceed with the following
elif [ $action = "rename" ]
then
echo "Please enter the filename you wish to rename"
read filename
# check if filename exists, if it doesn't then exit program
    if [ ! -e  $filename ] 
    then
    echo "$filename does not exist"
    exit 1
    fi
echo "Please enter the new filename"
read filenamenew
mv $filename $filenamenew
echo "$filename has been renamed to $filenamenew"
fi

echo "Do you want to perform another file operation (yes/no) ?"
read answer

if [ $answer = yes ]
then "run script again"
exit 0
    elif [ $answer = no ]
    then echo "Exiting Program"
    exit 0
    fi
fi

承認された回答:

エコーの前に「アクションを選択してください…」

answer=yes
while [ "$answer" = yes ]
do

最後に、交換します

if [ $answer = yes ]
then "run script again"
exit 0
    elif [ $answer = no ]
    then echo "Exiting Program"
    exit 0
    fi
fi

によって

if [ "$answer" = yes ]
then "run script again"
fi

done

echo "Exiting Program"
exit 0

私がやったことは、プログラムをwhile [$condition ] do ; ... done

条件が正常であることを確認します(answer=yes )最初のループで。


Linux
  1. Linux シェル スクリプトで Yes/No/Cancel の入力を求めるにはどうすればよいですか?

  2. シェルスクリプトの並列実行

  3. bash スクリプトをデーモンとして実行する

  1. 簡単なシェルスクリプトで配布名とバージョン番号を取得するにはどうすればよいですか?

  2. Linux –画面上でスクリプトを実行する方法ロック/ロック解除?

  3. PHPスクリプトが実行されているかどうかを確認するためのcronジョブ、実行されていない場合は実行しますか?

  1. スクリプトを実行する方法??

  2. Linuxスクリプトで端末のユーザー入力を隠す

  3. bash スクリプトの if 条件を否定する