Home > database >  Enable or disable a task in shell script
Enable or disable a task in shell script

Time:01-25

I have a few executable files to execute one by one. e.g.

A_1.sh
B_0.sh
cs_2.sh
c_4.sh
3_d.sh

I can execute all files with the following script, but I can't able to disable one, if I don't need that.

#!/bin/sh
for type in \
A_1 \
B_0 \
cs_2 \
c_4 \
3_d 
do
echo executing $type.sh
done

When I am disabling one e.g.

    #!/bin/sh
    for type in \
    A_1 \
    B_0 \
#    cs_2 \
    c_4 \
    3_d 
    do
    echo executing $type.sh
    done

It is showing syntax error near c_4. How to handle it actually?

CodePudding user response:

The simplest is to use Bash:

#!/bin/bash
arr=(
  A_1
  B_0
  # This is a comment
  # cs_2
  c_4
  3_d
) 
for type in "${arr[@]}"; do
    echo executing $type.sh
done

You can read from a here document and filter comments:

#!/bin/sh
while read -r -u 3 line; do
   # Is a comment?
   if grep '^ *#' <<<"$line"; then continue; fi
   # Close fd3, on the safe side.
   echo executing $type.sh <&3-
done 3<<EOF
A_1
B_0
# This is a comment
# cs_2
c_4
3_d
EOF 

Or invert the logic, do a function:

run() {
  echo executing $1.sh
}
run A_1
run B_0
# run cs_2
run c_4
run 3_d
  • Related