0

I am not so familiar with shell script syntax and protocols.

I have written following function which accepts

  • a command string as a mandetory parameter
  • ignore error as an optional parameter

--

function quit {
    \rm -f "~/script.lock"
    exit
}

function abnormal_quit {
    echo $'\n'
    echo "Script Execution Terminated Abnormally.."
    echo "STATUS :: FAIL"
    quit
}

function exec_cmd {
    command="$1"
    continue_on_error="true"
    if [ -z "$2" ]; then
        continue_on_error="false"
    fi

    echo "========================================================"
    echo "Executing command :-"
    echo "$command ....."

    if ${command[@]}
    then
        echo "Command Executed successfully with return code : $?"
        echo "COMMAND - $command"
        echo "==============================================================="
        echo $'\n'
    else
        echo "Failed to execute command with return code :- $?"
        echo "COMMAND - $command"
        echo "==============================================================="
        echo $'\n'
        if [ $continue_on_error == "false" ]; then
            abnormal_quit
        fi
    fi
}

log_file="output.log"
exec_cmd "ls -lrt >> $log_file"

If I execute above shell script it gives me following error

[root@localhost data]# sh test.sh
========================================================
Executing command :-
ls -lrt >> log.out .....
ls: >>: No such file or directory
ls: log.out: No such file or directory
Failed to execute command with return code :- 2
COMMAND - ls -lrt >> log.out
===============================================================

Script Execution Terminated Abnormally..
STATUS :: FAIL

The issue here is - The shell script assume "ls -lrt >> log.out" as a single command and the redirection arrows are considered as an filename argument to the "ls" command. Hence throws an error ">>: No such file or directory"

3
  • exec_cmd "ls -lrt" >> $log_file not work? Commented Sep 17, 2013 at 11:38
  • Bob I think it will output the result of exec_cmd function to the file, not ls -lrt Commented Sep 17, 2013 at 11:39
  • Imho, you should try with executing this command inside the script with command=`$1` Commented Sep 17, 2013 at 11:41

3 Answers 3

2

You can use eval to execute a command contained within a string. Try this:

if eval "$command"
then
Sign up to request clarification or add additional context in comments.

3 Comments

Did not work again... Can you please run the same script on your machine and let me know if it works for you?
yes, it worked perfectly for me. Did you use exactly what I said in my answer?
eval is a good sultion, I give it a plus. You can just write eval "$1" just for test inside your function and you will see it works.
0

Try executing the command with

command=`$1`

Remember the command will get executed the moment the script is in this line, not later and command variable will hold the results of this command execution

1 Comment

That's true. I want to know why it doesn't work though :) I made a question here: stackoverflow.com/questions/18849567/…
0

Since you are not familiar with the syntax and protocols. Please get to know them first. It is not too much to read like network protocols.

First of all - decide on the type of the shell that you want to use. Syntax do change based on this.

Secondly from the scripts requirement, make sure you verify your mandatory parameter with a simple if statement check, else things are bound to go down.

You already have the answer to your question as eval anyways.

1 Comment

Please learn the stackoverflow protocols and do not comment on the question which is already answered....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.