How to create a shell script that puts an echo at the beginning of each line in a file

Asked

Viewed 396 times

3

I need to create a script that inserts the echo command at the beginning of each line of the file and at the end >> blocodenotes, for example

#!/bin/bash
ls -l
uname -a
netstat -tunap

I want a script that turns into this:

echo "#!/bin/bash >> log"
echo "ls -l" >> log"
echo "uname -a" >> log" 
echo "netstat -tunap >> log"
  • With the command cat it is possible to write a block of instructions to a file using the simple resource document here.

2 answers

1

With sed is also possible:

sed -i -e 's/^/echo \"/;s/$/ >> log\"/'  arquivo

1

You can use the utility awk to solve your problem with just one line, see only:

$ awk '{ print "echo \"" $0 "\" >> log"  }' script.sh > saida.txt

Browser other questions tagged

You are not signed in. Login or sign up in order to post.