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

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

私は単純なファイル処理スクリプトを書いていますが、最後を除いてうまくいきます。私が言うことは、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

ベストアンサー1

echo「作業を選択してください...」前

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最初のループの条件がOK()であることを確認してください。

おすすめ記事