Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
595 views
in Technique[技术] by (71.8m points)

parsing - How to get exact command line string from shell?

I know about $*, $@, "$@" and even ${1+"@"} and what they mean.

I need to get access to the EXACT command-line arguments string from a shell script. Please pay attention to the quotes in the example. Anything like "$@" saves parameters but removes quotes and I don't see how to recover from this.

Example:

./my-shell.sh "1 2" 3

And I need to retrieve the EXACT parameter string without any processing:

"1 2" 3

Any idea how to achieve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In bash, you can get this from the shell history:

set -o history
shopt -s expand_aliases

function myhack {
  line=$(history 1)
  line=${line#*[0-9]  }
  echo "You wrote: $line"
}
alias myhack='myhack #'

Which works as you describe:

$ myhack --args="stuff" * {1..10}    $PATH
You wrote: myhack --args="stuff" * {1..10}    $PATH

Also, here's a handy diagram:

image


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...