I have a script

[pic@pic Desktop]$ cat skrypt.sh 

awk '{sub(/#echo "Wstałem"/,"#echo \"Wstałem\""); print $0 > "skrypt.sh"}' skrypt.sh
awk '{sub(/awk/,"#awk"); print $0 > "skrypt.sh"}' skrypt.sh

[pic@pic Desktop]$

I run the script

[pic@pic Desktop]$ ./skrypt.sh 
./skrypt.sh: line 5: h: command not found
[pic@pic Desktop]$ 

Why gets the message?

./skrypt.sh: line 5: h: command not found


After running the script I want to get the result


#awk '{sub(/#echo "Wstałem"/,"#echo \"Wstałem\""); print $0 > "skrypt.sh"}' skrypt.sh
#awk '{sub(/awk/,"#awk"); print $0 > "skrypt.sh"}' skrypt.sh

This is just an example. The idea is that in a real script after the call has been commented out the line above.

closed as unclear what you're asking by Braiam, jasonwryan, jimmij, slm Jan 24 '15 at 20:05

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Did you try to append the script path? – jherran Jan 24 '15 at 16:25
  • 1
    Your script looks very strange. Can you explain what you suppose to do? – Costas Jan 24 '15 at 16:28
  • 3
    You cannot modify script while executing it, and with > redirection you basically delete all lines from script and only then add some other stuff. It is not clear for me what are you really trying to do. – jimmij Jan 24 '15 at 16:29
  • Seems due to the two awk lines, operating on the script itself, a stray h character somehow ends up in the script which gets executed resulting in the error message. – mkc Jan 24 '15 at 16:52
  • Why you want to do this? What are you trying to accomplish? – Braiam Jan 24 '15 at 17:28

Very strace behaviour. But there is a logic behind.

bash reads the script line by line. The first awk statement just writes the skrypt.sh file new, with the same content, because there is nothing to replace.

The second awk statement replaces awk with #awk (single substitution). So, it adds one character. Then bash reads the rest of the file. The offset is positioned at the end of the line (with the second awk statement, line 4). But that line is now a bit longer (one character). So bash reads the rest of the line, which is a single h. That is threaded as a command (because we are in a bash script and it ends with a newline). And h, of course, is not a valid command, hence the error message.

To accomplish what you want (apart from that your script is very precarious) add a character more at the end of line 4, which cannot be interpreted as a command, a space () for example.

Last, but not least:

You shall not edit a scripts within itself.

Not the answer you're looking for? Browse other questions tagged or ask your own question.