0

Inside my shell script I have

#!/bin/bash
var1=$1
var2=$2
cat<<new_script<<EOF
...some code...
for i in `find / -perm 6000 -type f`; do chmod a-s $i; done
mkdir $var1
...some code...
EOF

I have tried a lot of things, but I am not able to escape the "find" command. Instead of writing it to the new_script, when I run this shell script, it just runs the find command in terminal.

Saurabh Gupta
  • 506
  • 1
  • 4
  • 18

1 Answers1

2

Quote EOF with single quotes:

#!/bin/bash
cat >>new_script <<'EOF'
...some code...
for i in `find / -perm 6000 -type f`; do chmod a-s $i; done
...some code...
EOF
Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • Can you explain what changes does putting 'EOF' do to make it work? – Saurabh Gupta Jul 21 '17 at 05:36
  • Without single quotes, behavior is variables are interpolated, commands in backticks are evaluated, etc. This can be disabled by quoting any part of the label, which is then ended by the unquoted value. [Source](https://en.wikipedia.org/wiki/Here_document#Unix_shells). – Cyrus Jul 21 '17 at 05:42
  • I need to use variables as well. I have made the edits. – Saurabh Gupta Jul 21 '17 at 05:57
  • 1
    Replace `'EOF'` by `EOF` and `\`find / -perm 6000 -type f\`` by `\$(find / -perm 6000 -type f)`. – Cyrus Jul 21 '17 at 16:46