简体   繁体   English

在shell脚本中获取输入

[英]Get input in shell script

I know to get an input with a shell script you can do 我知道要输入可以执行的shell脚本

echo "text"
read $VAR

or to get it at runtime 或在运行时获取

#! /bin/sh

if [ "$1" ]
then
  #do stuff
else
  echo "No input"
fi

But what if I want to get the entire string after I run it, including spaces? 但是,如果我想在运行后获取整个字符串(包括空格)怎么办? I could use quotation marks, but is there any way around this, so I can do: ./script.sh This is a sentence 我可以使用引号,但是有什么办法可以解决,所以我可以这样做: ./script.sh This is a sentence

Use 采用

#!/bin/sh

if [ $# -gt 0 ] ; then
    echo "$*"
else
    echo "No input"
fi

But first, think about what you're doing and just pass a single quoted parameter. 但是首先,请考虑您在做什么,只传递一个引用的参数即可。

./script.sh "This is a sentence"

另一种方法是

./script.sh This\ is\ a\ sentence

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

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