如果输入是 yes,则让脚本再次运行

如果输入是 yes,则让脚本再次运行

我正在编写一个简单的文件处理脚本,除了最后之外,它一切正常,我想说的是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我只是确保第一个循环中的条件正常 ( ) 。

答案2

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

#!/bin/sh
...
if [ yes = "$answer" ]; then
  /usr/local/bin/myscript
fi

在其他情况下,您不需要 ,exit 0因为这会自动发生。此外,如果您知道脚本结束时的工作目录与开始时的工作目录相同,那么您可以避免对脚本路径进行硬编码,而只需使用$0

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

#!/bin/sh
...
if [ yes = "$answer" ]; then
  exec /usr/local/bin/myscript # or exec $0
fi

这就像以前一样,除了第一个进程在启动第二个进程时退出,当第二个进程完成时,如果它没有生成第三个进程,我们直接返回到启动第一个进程的人,大概是 shell。

答案3

尝试将脚本包装为函数并递归调用它:

#/bin/bash
do_file_action(){
  echo "Select an option from copy , remove , rename , linking"
  #read in user input into the action variable

  read action

  ...

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

  if [ $answer = yes ]
  then do_file_action
  exit 0
      elif [ $answer = no ]
      then echo "Exiting Program"
      exit 0
      fi
  fi
}

do_file_action

相关内容