読み取りコマンド:ユーザーが何かを入力したことを確認する方法

読み取りコマンド:ユーザーが何かを入力したことを確認する方法

ユーザーが何かを入力したことを確認するためにif elseステートメントを作成しようとしています。持っている場合はコマンドを実行する必要があり、そうでない場合はヘルプステートメントをエコーし​​たいと思います。

ベストアンサー1

(非常に単純な)例は次のとおりです。次のコードを含むuserinputという名前のファイルが生成されます。

#!/bin/bash

# create a variable to hold the input
read -p "Please enter something: " userInput

# Check if string is empty using -z. For more 'help test'    
if [[ -z "$userInput" ]]; then
   printf '%s\n' "No input entered"
   exit 1
else
   # If userInput is not empty show what the user typed in and run ls -l
   printf "You entered %s " "$userInput"
   ls -l
fi

Bash 学習を開始するには、次のリンクを確認することをお勧めします。http://mywiki.wooledge.org/

おすすめ記事