0

The output of this code seems to depend on the option I give (-n or -a) but I simply don't understand the reason. Here's the portion of the code that doesn't work (just the code itself + outputs):

#!/bin/sh

FILE="/path/to/some/textfile"

[ $# -eq 0 ] && echo "No arguments. Exiting.." && exit 1
[ $1 != "-n" ] && [ $1 != "-a" ] && echo "No new default usernames. Exiting" && exit 1
echo $@
echo
newusernames=`echo "$@"|cut -d' ' -f2-`
printf "'$newusernames' $mode as default usernames in $FILE\n"

Running it and giving some arguments in both ways:

scriptname -a word1 word2 word3

Gives:

-a word1 word2 word3

'word1 word2 word3'  as default usernames in path/to/textfile

While..:

scriptname -n word1 word2 word3

Gives:

word1 word2 word3
'word2 word3'  as default usernames in path/to/textfile

$mode is just a var that should be set in either two cases. As shown the output is really different while this shouldn't happen.

Please note that I'm aware that this is not the most correct way to pass shell arguments in a script. I'm just trying to understand the reason for this behaviour.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
sirakai
  • 5
  • 3
  • `[ $1 != "-n" ]` is quoting the wrong thing. You don't need to quote `-n`; you _do_ need to quote `$1`. Thus, `[ "$1" != -n ]` – Charles Duffy Mar 25 '21 at 22:06
  • ...otherwise, `yourprogram '*'` will let the `*` be replaced with a list of filenames in the current directory when `[` tries to run. – Charles Duffy Mar 25 '21 at 22:06
  • Beyond that, though... could you please try to re-title this so the title is itself a self-contained question? Something that talks about "this shell code" is a question that can't be understood until _after_ someone clicks through and reads the body, but the purpose of a title is to let someone figure out if the content in the body is interesting to them. – Charles Duffy Mar 25 '21 at 22:07
  • `echo $@` likewise is buggy -- see further discussion of why quotes are essential even for `echo` at [I just assigned a variable, but `echo $variable` shows something else`](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) – Charles Duffy Mar 25 '21 at 22:09

1 Answers1

0

Your second case expands to this:

echo -n word1 word2 word3

and echo -n can be special (and seems to be, in this case): it's interpreted as an option to echo to not print a trailing newline character.

One solution could be to use

shift
newusernames=$(echo "$*")

to remove the parameter first, or even more robust:

shift
newusernames=$(printf '%s' "$*")

In Bash, you'd have fancier options such as

printf -v newusernames '%s' "${*:2}"

to do everything in one step.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116