0

I hope you're having a great weekend,

I'm trying to create a script that execute a for only if a variable is not empty and if the variable is empty execute the command just one time, something like the following:

#!/bin/bash
X=$1
function execute
{
if [ ! -z $X ]
then
  $*
fi
}
execute for count in 1 2 3 4
execute do
  execute echo $count
  echo $(hostname)
execute done
2
  • 2
    Please paste your script first at shellcheck.net and try to implement the recommendations made there. Commented Aug 1, 2020 at 16:48
  • Based on other comments, you're essentially trying to make each command or keyword conditional. This is likely an X/Y problem of some kind. Why do you want to do it this way, and why do you feel like this is the best way to accomplish the task? Commented Aug 1, 2020 at 20:09

1 Answer 1

1

Quote Marks are Your Friends

You need to quote your variables, and you should also use the Bash test construct unless portability is an issue. There may be other problems with your code, but this refactoring should solve the problem you're specifically asking about.

#!/bin/bash

x="$1"

execute () {
    if [[ -n "$x" ]]; then
      "$@"
    fi
}

for count in {1..4}; do
  execute echo "$count"
  echo $(hostname)
done

Depending on your hostname, this will output something similar to:

1
localhost.local
2
localhost.local
3
localhost.local
4
localhost.local
Sign up to request clarification or add additional context in comments.

4 Comments

I want to execute different commands into the "for" if the variable is not empty, so if the variable is filled I want to enable the "for" lines
@JonathanViana Bash doesn’t have lambdas, so you’re working against the language by trying to pass a loop around. You also can’t make language keywords conditional the way you seem to expect. You could pass around strings to eval, but that’s not safe if you don’t trust or validate user input. In general, test your condition then call all associated commands.
I'm looking for an option to do the following, for example if the variable x is not empty execute the lines that have the word "execute" to run the commands as many times the the "for" required else just execute the commands and omit the lines with execute, I need something like this because I will run a lot of sentences and probably I can to an if/else an copy paste all the sentences. #!/bin/bash x="$1" execute () { if [[ -n "$x" ]]; then "$@" fi } execute for count in {1..4}; do execute echo "$count" commands_sentences_toe_xecute execute done
@JonathanViana I get it, but you're trying to make each keyword conditional, which isn't how bash works. You can implement conditional branching in valid bash, but making each line conditional will require you to eval it, or write your own parsing routines. This is likely an X/Y problem of some kind. Why do you want to do whatever you're doing?

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.