_exec()
{
CMD="$1"
echo "Executing command: $CMD"
EXEC_OUT=`$CMD`
EXEC_RET=$?
}
_exec "hostname|cut -d\".\" -f1 "
SHORT_NAME=$EXEC_OUT
if [ $EXEC_RET -ne 0 ];then
echo "Unable to determine shortname of server"
else
echo $EXEC_OUT
fi
-
1Welcome to SuperUser. What is the error you are getting? How do you call the function? What have you tried already?– mtakAug 26, 2014 at 10:52
-
these are my debug results?– Niraj NandaneAug 26, 2014 at 11:05
-
+ _exec 'hostname|cut -d"." -f1' + CMD='hostname|cut -d"." -f1' + echo 'Executing command: hostname|cut -d"." -f1' Executing command: hostname|cut -d"." -f1 ++ 'hostname|cut' '-d"."' -f1 te.sh: line 5: hostname|cut: command not found + EXEC_OUT= + EXEC_RET=127 + SHORT_NAME= + '[' 127 -ne 0 ']' + echo 'Unable to determine shortname of server' Unable to determine shortname of serve– Niraj NandaneAug 26, 2014 at 11:05
-
when the function puts $CMD to execute it puts 'hostname|cut' '-d"."' -f1 te.sh– Niraj NandaneAug 26, 2014 at 11:08
-
I suggest you post these answers in your original question and read in the help section about formatting.– mtakAug 26, 2014 at 14:27
Add a comment
|
1 Answer
You need to evaluate your cmd on your exec function.
EXEC_OUT=`eval $CMD`
Not sure about why though. I think it's because there are several commands in a single variable. Maybe someone more experimented can confirm this.
See eval man page.