I've created a shell script that has the SSH address as the first argument, then has one or more arguments afterwards. The script is capable of working with two arguments or less (the address and the single argument), but it is not able to work properly after. Below is my code to get a better idea.
ssh.sh $1 << EOF
$(typeset -f sr_single "$@")
if [ "$#" -eq 2 ]; then
echo $2
sr_single $2
elif [ "$#" -lt 2 ]; then
echo "Needs at least two arguments: serial number and argument(s)"
else
echo "${@:2}"
for i in "${@:2}"; do
echo "'" $i "'"
sr_single $i
done
fi
EOF
Below is what it returns if I call the function "sr.sh [email protected] -l"
-l
And below is what it returns when I call "sr.sh [email protected] -l -v"
-l -v
' '
My question is how is this function not getting the second variable and the ones after on this one, where it seems to be working properly in the rest of the program? Thanks
$(typeset -f sr_single "$@")? It's strange that you have a command substitution without assigning the output to anything.sr_singleto pass the entire code. And, of course, fix your quoting -- which is to say, remove every single unquoted expansion../yourscript host _ '$(rm -rf ~)', the$2expansion inside the heredoc would be substituting that as code, with the results one might expect. Always, always try to keep code and data out-of-band from each other -- and when you can't,printf '%q'is your friend.