2020/12/15

Required Input On Bash

在寫 shell script 的時候有時候必須填寫必要參數,如果想要 ui friendly 的話可以使用 prompt 的互動方式請 user 填寫。

ask.sh
#!/bin/bash  
  
while [[ -z ${name} ]]; do  
  read -rp "your name: " name  
done  
  
while [[ -z ${age} ]]; do  
  read -rp "your age: " age  
done  
  
echo "Hello ${name^^}, your age is ${age}"

上方的執行過程如下:

$ /bin/bash ask.sh
your name: chan wu
your age: 40
Hello CHAN WU, your age is 40

沒有留言: