简体   繁体   English

如何在bash脚本中将第一行用户类型读入终端

[英]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. 进入终端,。/ cpc将在其中运行脚本,-c是$ option,test1.txt是$ source,备份是$ 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 参数存储在编号参数$1$2等中。因此,只需分配它们即可

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

See also man getopt or getopts in man bash . 另请参阅man getoptman bash getopts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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