Reputation: 3910
I have a bash script that looks like this:
if [ $# -eq 2 ]; then
REDIS_HOME=$2
else
if [ $# -eq 3 ]; then
REDIS_HOME=$2
PORTS=$3
else
REDIS_HOME="/usr/local/redis"
fi
fi
case "$1" in
start)
if [ -z "$PORTS" ]; then
cmd="$REDIS_HOME/bin/redis-server $REDIS_HOME/redis-6379.conf"
$cmd
else
IFS=",";
for port in $PORTS
do
cmd="$REDIS_HOME/bin/redis-server $REDIS_HOME/redis-$port.conf"
$cmd
done
fi
;;
*)
;;
esac
exit 0
When I run the script with my_script.sh start
, it works well by using the default redis home in the script. But when I ran it with my_script.sh start /usr/local/redis 6379
, it says "/usr/local/redis/bin/redis-server /usr/local/redis/redis-6379.conf" No such file or directory
.
Basically, I'm passing in the same REDIS_HOME
, but I cannot figure out why the script cannot resolve the path if it's passed in as a parameter.
Upvotes: 0
Views: 1180
Reputation: 781848
IFS=,
is causing the problem. When you do
$cmd
it uses $IFS
to break the expansion of $cmd
into words. Since space is not in $IFS
, the space is not treated as a delimiter between the program name and the argument, so the entire result is treated as the program name. And of course it's not found.
I'm not sure why you need the $cmd
variable in the first place. You can just do:
IFS=",";
for port in $PORTS
do
$REDIS_HOME/bin/redis-server $REDIS_HOME/redis-$port.conf
done
Upvotes: 2