简体   繁体   中英

How to read the first line user types into terminal in bash script

I'm trying to write a script where to run the script, the user will type something along the lines of

$./cpc -c test1.txt backup

into the terminal, where ./cpc is to run the script, -c is $option, test1.txt is $source and backup is $destination.

How would I assign the values typed in to the terminal to use them in my script, for example in

if [[ -z $option || -z $source || -z $destination ]]; then
    echo "Error: Incorrect number of arguments." (etc)

as when checking the script online the following errors return: 'option/source/destination is referenced but not assigned.'

Sorry in advance if any of this doesn't make sense, I'm trying to be as clear as possible

The arguments are stored in the numbered parameters $1 , $2 , etc. So, just assign them

option=$1
source=$2
destination=$3

See also man getopt or getopts in man bash .

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