如果输入为是,则获取脚本以再次运行

joh*_*345 5 shell shell-script

我正在整理一个简单的文件处理脚本,除了最后,它一切正常,我想说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
Run Code Online (Sandbox Code Playgroud)

Arc*_*mar 8

在 echo “选择一个动作...”之前

answer=yes
while [ "$answer" = yes ]
do
Run Code Online (Sandbox Code Playgroud)

最后,替换

if [ $answer = yes ]
then "run script again"
exit 0
    elif [ $answer = no ]
    then echo "Exiting Program"
    exit 0
    fi
fi
Run Code Online (Sandbox Code Playgroud)

经过

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

done

echo "Exiting Program"
exit 0
Run Code Online (Sandbox Code Playgroud)

我所做的是将程序封装在一个while [$condition ] do ; ... done.

我只是确保answer=yes在第一个循环中条件正常 ( )。


dub*_*jim 6

关于循环的答案是处理这个问题的好方法。但是作为参考,让脚本重新调用本身并没有错,因此/usr/local/bin/myscript可以阅读:

#!/bin/sh
...
if [ yes = "$answer" ]; then
  /usr/local/bin/myscript
fi
Run Code Online (Sandbox Code Playgroud)

exit 0在另一种情况下您不需要 an ,因为那会自动发生。此外,如果您知道您在脚本末尾与开始时在同一工作目录中,那么您可以避免硬编码脚本的路径,而只使用$0.

最后一个改进很重要。正如所写,第一次启动的脚本进程将产生第二个进程,并等待它完成;然后第二个可能会产生第三个并等待它完成;等等。这会消耗资源,并且当它们的后代退出时,这些脚本实际上没有任何工作要做。所以你最好在编程中使用相当于“尾调用”的方式来运行它们。这是使用以下命令完成的exec

#!/bin/sh
...
if [ yes = "$answer" ]; then
  exec /usr/local/bin/myscript # or exec $0
fi
Run Code Online (Sandbox Code Playgroud)

这和以前一样工作,除了第一个进程在启动第二个进程时退出,当第二个进程结束时,如果它没有产生第三个进程,我们直接回到启动第一个进程的人,大概是 shell。

  • +1 as exec 是要走的路,在这里(否则每个内部“/.../.../myself”将添加另一个层/子外壳) (2认同)