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 に答える