Using a loop to execute a text file

I have redirected some valuable information to a text file. How can I execute each line of this text file in a loop?

What am I thinking about the double space of my text file and then use a loop to execute each line. I hope that when I double the space in the text file, each command line will have its own line.

For example, this text file:

cat / etc / passwd | head -101 | tail -3 nl / etc / passwd | head -15 | cut -d ':' -f1 cat / etc / passwd | cut -d ':' -f1,5 | tee users.txt nl / etc / passwd | tail -1 | cut -f1 ls ~ / home | nl | tail -1 | cut -f1 ls -lR / 2> / dev / null | sort -n -r +4 | head -1

should look like this when I double it:

cat / etc / passwd | head -101 | tail -3
nl / etc / passwd | head -15 | cut -d ':' -f1
cat / etc / passwd | cut -d ':' -f1,5 | tee users.txt
nl / etc / passwd | tail -1 | cut -f1
ls ~ / home | nl | tail -1 | cut -f1 ls -lR / 2> / dev / null | sort -n -r +4 | head -1

And then I would use a loop to execute each line.

here is my script:

FILE="$1"

echo "You Entered $FILE"

if [ -f $FILE ]; then

tmp=$(cat $FILE | sed '/./!d' | sed -n '/regex/,/regex/{/regex/d;p}'| sed -n '/---/,+2!p' | sed -n '/#/!p' | sed 's/^[ ]*//' | sed -e\
s/[^:]*:// | sed -n '/==> /!p' | sed -n '/--> /!p' | sed -n '/"lab3.cmd"/,+1!p'\
 | sed -n '/======/!p' | sed -n '/regex/!p' | sed -n '/commands are fun/\
!p' | sed -n '/regex/!p')

fi

MyVar=$(echo $tmp > hi.txt)
echo "$MyVar"
      

Is this happening correctly?

0


source to share


2 answers


Why not just split the line separators into commands? You don't need a blank line to delimit them if your command doesn't need line separators - and then what if you need to contain 2 lines?



+1


source


I'm afraid I don't understand the problem you are trying to solve. If you are writing commands to a file named commands.txt you can execute them simply

sh commands.txt

      

Executes every line of the file. If you want to do it in a loop, I suppose you can try something like



for i in *; do FILE="$i" sh commands.txt; done

      

Perhaps you can edit your question to make it more specific?

0


source







All Articles