【问题标题】:While loop restart if condition isn't meant如果条件不意味着循环重新启动
【发布时间】:2022-12-17 11:04:54
【问题描述】:

如果用户输入的响应与允许的选择不匹配,但第二次输入正确的响应,我试图让这个脚本循环并重新启动。我尝试使用 continue,但它会无限循环。有什么想法吗?

`

#!/bin/bash

#Obtaing user selection and input
echo " Gathering list of users on this machine..."
sleep 2
echo "$( ls /Users )" 
echo "From the list above, which user did you want to work with?"
read userSelection
echo "What is that user's password?"

#Hiding User's Password
stty -echo
read userSelectionPassword
stty echo
echo "Did you want [enable], [disable], or check the current [status] of Secure Token for $userSelection?"
read taskSelection


#Converting input to lowercase
taskSelectionLower=$(echo $taskSelection | tr '[:upper:]' '[:lower:]')


#Running commands
while [ true ]
do
if [[ $taskSelectionLower == "enable" ]]; then
    echo "Enabling..."
    echo "$(sysadminctl -adminUser AdminUser -adminPassword AdminPass -secureTokenOn $userSelection -password $userSelectionPassword)"
    break
elif [[ $taskSelectionLower == "status" ]]; then
    echo "Displaying $userSelection current Secure Token status..."
    echo "$( sysadminctl -secureTokenStatus $userSelection )"
    break
elif [[ $taskSelectionLower == "disable" ]]; then
    echo "Disabling..."
    echo "$(sysadminctl -adminUser AdminUser -adminPassword AdminPass -secureTokenOff $userSelection -password $userSelectionPassword)"
    break
else
    echo "Incorrect selection made..."
    echo "Did you want [enable], [disable], or check the current [status] of Secure Token for $userSelection?"
    read taskSelection
    exit
fi

done

`

尝试在条件结束时使用 continue,但无限循环。

预期结果是 for 循环重新启动,允许用户输入正确的响应并获得正确的输出。

【问题讨论】:

  • [ true ] 总是成功,因为 true 是一个非空字符串。 while [ false ]; do 的工作方式完全相同。你可以执行命令truewhile true; do ,或者使用总是成功的标准命令:while :; do
  • 您正在读取 taskSelection 的新值,但永远不会重新定义 taskSelectionLower 的值,这是您所有检查所查看的内容。
  • (无论如何,exit 会导致您的脚本退出而根本不重复循环。)
  • @chepner 谢谢你就是这样!!!!现在我必须弄清楚如何实现一个列表来存储系统返回的值。祝我好运!!

标签: bash if-statement while-loop scripting applescript


【解决方案1】:

select 命令正是为这种交互式脚本设计的。

一些希望有用的观察结果——

echo " Gathering list of users on this machine..." 
sleep 2 # why?

这个sleep 似乎没有任何作用,只会惹恼用户。

echo "$( ls /Users )"  # why?? Don't do this.

这与

ls /Users # same output. Keep it simple.

另外,因为你是bash而不是sh,而不是

taskSelectionLower=$(echo $taskSelection | tr '[:upper:]' '[:lower:]')

尝试将变量声明为小写 -

declare -l taskSelectionLower
read taskSelectionLower

或者用parameter parsing syntax处理它 -

read taskSelectionLower
taskSelectionLower="${taskSelectionLower,,}"

但为什么要让他们可能打错字呢?
考虑使用 select 语句。

与其构建自己的循环、提供选项、担心格式化等,不如让已经完成这些工作的工具为您处理。

我使用 echo 重写以显示要执行的命令 - 将它们取出,它应该可以正常运行。

#!/bin/bash
cd /c/Users
echo "Please select a valid user from this system."
select u in */; do
  if [[ -d "$u" ]]
  then userSelection="${u%/}"; break
  else echo "Please enter a number from the presented options."
  fi
done # get just the username
cd $OLDPWD # if needed
admSet() { # no reason to ask for the password just to get status...
  read -sp "what's $userSelection's password? " userSelectionPassword
  echo # reading the password leaves the cursor on the previous line
  echo sysadminctl -adminUser AdminUser -adminPassword AdminPass 
         -secureToken$1 "$userSelection" -password "$userSelectionPassword"
}
select taskSelection in enable disable status; do # case is controlled
  case "$taskSelection" in
    status) echo "Displaying $userSelection current Secure Token status..."
            echo sysadminctl -secureTokenStatus $userSelection ;;
    enable) echo "Enabling..."  ; admSet On  ;;
   disable) echo "Disabling..." ; admSet Off ;;
         *) echo "Please enter a number from the presented options."
            continue ;;
  esac
  break
done

【讨论】:

    猜你喜欢
    • 2021-09-05
    • 1970-01-01
    • 2022-12-18
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多