2

我不确定在这个过程中下一步是什么让命令做我想做的事。我想选择一个字母来执行命令。现在它可以让你使用任何字母。

#!/bin/bash
echo "Please select l to list files of a directory, b to backup a file or directory, u to edit a user's password, and x to exit the script"

read $answer

if [ $answer="l" ]; then

printf "Please select folder:\n"
select d in */; do test -n "$d" && break; echo ">>> Invalid Selection"; done
cd "$d" && pwd

ls

fi
4

2 回答 2

1

使用案例陈述

case expression in
    pattern1 )
        statements ;;
    pattern2 )
        statements ;;
    ...
esac

例如:

case $arg in
    l)
        printf "Please select folder:\n"
        select d in */; do test -n "$d" && break; echo ">>> Invalid Selection"; done
        cd "$d" && pwd
        ls
        ;;
    cmd1)
        echo "Some other cmds line 1"
        echo "Some other cmds line 2"
        ;;
    -q) exit;;
    *) echo "I'm the fall thru default";;
esac
于 2012-04-19T00:25:48.803 回答
0

您可以select为此使用内置函数,它可以让您对每个选项使用数字而不是字母,但会负责读取和验证输入:

select cmd in \
  "List files of a directory" \
  "Backup a file or directory" \
  "Edit a user's password" \
  "Exit";
do
  case $cmd in
  1) do_list_files ;;
  2) do_backup_files ;;
  3) do_edit_password ;;
  4) exit 0 ;;
  esac
done

PS3您可以通过设置变量来更改提示(例如PS3="Your choice? "

于 2012-04-19T14:27:36.223 回答