简体   繁体   中英

Enter inputs taken by a bash script automatically

I need to pass two inputs to my bash script enter_name.sh . The first one is Y or N . The next one is the name of the user.

I tried echo "Harry\\nY\\n" | bash enter_name.sh echo "Harry\\nY\\n" | bash enter_name.sh but it doesn't work.

To check if the first argument is y/n, and second argument is not empty:

#!/usr/bin/env bash

[[ "$1" = [yYnY] ]] || { echo "First argument must be y/n"; exit 1; }
[ -z "$2" ] && { echo "Second argument must be a user name"; exit 1; }
echo $1 $2

Example:

$ ./enter_name.sh 
First argument must be y/n
$ ./enter_name.sh a x
First argument must be y/n
$ ./enter_name.sh N
Second argument must be user name
$ ./enter_name.sh N some_user
N some_user

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM