0

I am attempting to write a script that will read through a text file, and then execute every line that begins with the word "run" or "chk" as a command. This is what I have thus far:

#!/bin/bash
counter=1
for i in $@
do
    while read -r line 
    do
    if [[ ${line:0:4} == "run " ]]
    then
        echo "Now running line $counter" 
        ${line:4:${#line}}
    elif [[ ${line:0:4} == "chk " ]]
    then
        echo "Now checking line $counter" 
        ${line:4:${#line}}
    elif [[ ${line:0:2} == "# " ]]
    then
        echo "Line $counter is a comment"
    else
        echo "Line $counter: '$line' is an invalid line"
    fi
    counter=$((counter+1))
    done<$i
done

However, when I feed it a text file with, for example the commands

run echo > temp.txt

It does not actually create a file called temp.txt, it just echoes "> temp.txt" back to the stdout. It also does a similar thing when I attempt to do something like

run program arguments > filename.txt

It does not put the output of the program in a file as I want, but it rather tries to treat the '>' as a file name.

I know this is a super specific and probably obvious thing, but I am very new to bash and all shell scripting.

Thanks

5
  • 1
    Most special characters are not processed when expanding variables. You need to use eval to do all the normal shell parsing. Commented Feb 4, 2017 at 18:39
  • would that make the substrings register as commands? Commented Feb 4, 2017 at 18:42
  • eval executes a string just as if it were a normal shell command. Commented Feb 4, 2017 at 18:43
  • $@ needs to be enclosed in double quotes as "$@" to prevent word splitting and globbing. Commented Feb 4, 2017 at 18:53
  • Say one of the commands is an if statement that spans multiple lines. Is there a way to read it as one command instead of the while loop reading each line as a separate command? Commented Feb 4, 2017 at 19:42

1 Answer 1

2

You need to use eval to do all the normal shell parsing of the variable:

eval "${line:4}"

You also don't need :${#line}. If you leave out the length, it defaults to the rest of the string.

Sign up to request clarification or add additional context in comments.

4 Comments

It might be useful commenting on what eval will do with the quoting and potential expansions on its arguments.
@Fred It just treats them exactly the same as if they were in the script itself.
I see, that makes sense. Thank you
@Barmar I was not really clear with my comment. The potential difficulty is when one (for any reason) tries to make up that string as part of a shell statement (i.e. a variable assignment). It can be a bit frustrating when you are not knowledgeable enough. It is not directly related to the question though.

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.