Skip to content

Instantly share code, notes, and snippets.

@tateisu
Last active November 1, 2019 19:52
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 tateisu/391d33293ac1a874a35b13206efc0a74 to your computer and use it in GitHub Desktop.
Save tateisu/391d33293ac1a874a35b13206efc0a74 to your computer and use it in GitHub Desktop.
#!/bin/ash
echo "count=$#, 1=$1, 2=$2"
#!/bin/ash
print(){
echo "count=$#, 1=$1, 2=$2"
}
CMD="print"
# CMD= "./print.sh"
###################################
# $(test && echo) を使う場合
echo "testing test && echo ..."
eval "$CMD $(true && echo \"--a --b\")"
eval "$CMD \"$(true && echo --a --b)\""
eval "$CMD $(false && echo \"--a --b\")"
eval "$CMD \"$(false && echo --a --b)\""
# NG 空の引数ができてしまう
###################################
# 一時変数を使う場合
echo "testing temporary variable..."
if true ;then
HEADER='"--a --b"'
else
HEADER=
fi
eval "$CMD $HEADER"
if false ;then
HEADER='"--a --b"'
else
HEADER=
fi
eval "$CMD $HEADER"
@tateisu
Copy link
Author

tateisu commented Nov 1, 2019

sh -c "..." か eval "" を使うことでコマンド文字列の解釈が再度行われる

@tateisu
Copy link
Author

tateisu commented Nov 1, 2019

sh -c だと関数呼び出しに使いにくいのでevalにした
どちらも変数展開が再度行われるなどあり、危険性は高い

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