I have a bash script I'm writing that is not concatenating a string as expected. I have (in the script) a preset list of folder paths in a single string, separated by a single space. This works great, later on in the script, I can cycle through each folder path as expected.
I am trying to allow the user to enter in an optional list of other folders, and add to the list of paths to check for. Below is my code, and the output of the command:
#!/bin/sh
MOUNTPOINTS="/ /var /var/moodledata2" #Set the mount points to check for
while getopts ":dh" opt; do
case $opt in
d)
DEBUG=true
;;
h)
echo USAGE: diskspace \[-d\] \[\/path1 ...\]
exit 0
;;
\?)
echo Incorrect syntax
;;
esac
shift $((OPTIND - 1))
done
if [ "$#" -gt 0 ]; then
for var in $@; do
NEWMOUNTPOINTS .= $var
echo "${MOUNTPOINTS} ${var}"
echo "${NEWMOUNTPOINTS}"
done
fi
When running the command...
$ ./dt /home
./dt: 22: ./dt: NEWMOUNTPOINTS: not found
/ /var /var/moodledata2 /home
I originally tried with MOUNTPOINTS .= ${var}
and MOUNTPOINTS .= "${var}"
, but they still produce the same error. I thought I could create a new variable, but that didn't work either (as shown above). The rest of the script continues on, but only with the original MOUNTPOINTS
at the top; it never changes that, even if I use MOUNTPOINTS .= $var
, above.
What can I do to get a dynamic string created with the command line arguments supplied?
shift
ing like that in thewhile
loop is actually working the way you expect. I think you want to do that after the loop is finished. – Etan Reisner Jan 6 '15 at 23:08