0
_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
5
  • 1
    Welcome to SuperUser. What is the error you are getting? How do you call the function? What have you tried already?
    – mtak
    Aug 26, 2014 at 10:52
  • these are my debug results? Aug 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 Aug 26, 2014 at 11:05
  • when the function puts $CMD to execute it puts 'hostname|cut' '-d"."' -f1 te.sh Aug 26, 2014 at 11:08
  • I suggest you post these answers in your original question and read in the help section about formatting.
    – mtak
    Aug 26, 2014 at 14:27

1 Answer 1

0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .