Skip to content

Instantly share code, notes, and snippets.

@phith0n
Last active January 13, 2021 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phith0n/808127b8d0db8742d94a560e4ecb9b02 to your computer and use it in GitHub Desktop.
Save phith0n/808127b8d0db8742d94a560e4ecb9b02 to your computer and use it in GitHub Desktop.
一个离奇的Shell问题(请在Linux下运行,MacOS下运行会出错)
root@b3324f24e6c0:/tmp# ./sample.sh -n Bob -a
Arg: -n
Arg: Bob
Bob
Arg: -a
-a
Arg: --
--
#!/bin/bash
opt=`getopt -a -o "n:a" --long "name:,admin" -- "$@"`
eval set -- "$opt"
while true; do
echo "Arg: $1"
echo "$1"
shift
if [[ "$1" == "" ]]; then
break
fi
done
@phith0n
Copy link
Author

phith0n commented Jan 13, 2021

output.txt里,第3行为何不是-n,而是下一个循环里的Arg: Bob

@phith0n
Copy link
Author

phith0n commented Jan 13, 2021

原因是-n被作为参数了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment