I have a script for school that mimics the cp command. Part of the assignment is to modify the script so that the while
loop (that assigns stdin
to the variable filelist
) is instead done with sed.
The last argument needs to be saved to the variable to
because that could be a directory depending on other user input in the script.
while [ "$#" -gt 1 ]
do
filelist="$filelist $1"
shift
done
to="$1"
Is this finished product good? How could I polish it up?
filelist=$(echo $* | sed 's/ [^ ]*$//')
to=$(echo $* | sed 's/^.* \([^ ]*\)$/\1/')
while
loop produces different output, " 1 2 3" vs. "1 2 3" by thesed
call. – ferada Dec 5 '14 at 13:52to
assignment.len=$#; to=${!len}
orlen=$#; a=("$@"); to=${a[$# - 1]}
. – Etan Reisner Dec 5 '14 at 16:39