LoginSignup
0
0

More than 1 year has passed since last update.

Linux ユーザー入力で文字列や選択を要求する

Posted at

初めに

以下 2 つを試しました。

  • 文字列のユーザー入力
  • 選択肢を表示する

コード例

sample.sh
# while true で無限ループし、break や exit の処理を行わなかった場合、ユーザー入力処理を繰り返す
while true
do
  # ユーザー入力を待ち、入力された文字列を source_file という変数に格納
  read -p 'Input copy source file (Entering ? will display a list of files and directories):' source_file
  if [ "$source_file" = '?' ]; then
    # 「?」 が入力された場合、ls コマンドを実行する
    ls
  elif [ "$source_file" = '' ]; then
    # 空文字が入力された場合
    echo "An empty word was entered. Please retry."
  elif [ -e "$source_file" ]; then
    # ファイルが存在する場合、
    break
  else
    # 処理を中止する
    echo 'Source file does not exist.'
    exit 1
  fi
done

# ~/sample ディレクトリ以下を選択肢にする
select destination in `ls ~/sample`
do
  if [ `echo $destination | grep 'do_not_copy'` ]; then
    # ディレクトリ名に「do_not_copy」という文字列が含まれている場合、コピーできない旨を表示し、再度ユーザーに選択を求める
    echo "You cannot copy to $destination."
  elif [ "$destination" = "" ]; then
    # 空文字が入力された場合、選択肢の番号から選ぶことを要求し、再度ユーザーに選択を求める
    echo "You need to select 1)-`ls ~/sample | wc -l`)."
  else
    # コピー先が妥当な場合、コピーを実行し、処理を終える
    cp $source_file ~/sample/$destination
    echo "You copied $source_file to $destination."
    break
  fi
done

動作確認

  • コピー元ファイルが存在し、コピー先ディレクトリが正当な場合
[ec2-user@ip-172-31-8-7 ~]$ sh sample.sh
Input copy source file (Entering ? will display a list of files and directories):source_file.txt
1) child_1
2) child_2
3) child_do_not_copy
#? 1
You copied source_file.txt to child_1.
[ec2-user@ip-172-31-8-7 ~]$ ls ~/sample/child_1
source_file.txt <===ファイルがコピーされたことを確認
  • コピー元ファイルが存在しない場合
[ec2-user@ip-172-31-8-7 ~]$ sh sample.sh
Input copy source file (Entering ? will display a list of files and directories):hello
Source file does not exist.
  • コピー元ファイル入力で空文字を入力した場合
[ec2-user@ip-172-31-8-7 ~]$ sh sample.sh
Input copy source file (Entering ? will display a list of files and directories):
An empty word was entered. Please retry.
Input copy source file (Entering ? will display a list of files and directories):
  • コピー元ファイル入力で ? を入力した場合
[ec2-user@ip-172-31-8-7 ~]$ sh sample.sh
Input copy source file (Entering ? will display a list of files and directories):?
cat_eof_output.txt  cat_eof.sh  sample  sample.sh  source_file.txt
Input copy source file (Entering ? will display a list of files and directories):
  • 選択で child_do_not_copy を選択した場合
[ec2-user@ip-172-31-8-7 ~]$ sh sample.sh
Input copy source file (Entering ? will display a list of files and directories):source_file.txt
1) child_1
2) child_2
3) child_do_not_copy
#? 3
You cannot copy to child_do_not_copy.
#?
  • 選択肢にない番号を入力した場合
[ec2-user@ip-172-31-8-7 ~]$ sh sample.sh
Input copy source file (Entering ? will display a list of files and directories):source_file.txt
1) child_1
2) child_2
3) child_do_not_copy
#? 4
You need to select 1)-3).
#?

参考記事

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0