Reputation: 5950
In a bash script, I need to create another script file, So I have the following
cat >/etc/script2.sh <<EOL
#!/bin/bash
mysql -e "SHOW TABLES FROM $DATABASE_NAME" > /var/lib/mysql-files/tables.txt
echo "$(tail -n +2 /var/lib/mysql-files/tables.txt)" > /var/lib/mysql-files/tables.txt
EOL
However, the echo
at line 5 is executed and the output is added to script2.sh
How can I have script2
to look like
#!/bin/bash
mysql -e "SHOW TABLES FROM $DATABASE_NAME" > /var/lib/mysql-files/tables.txt
echo "$(tail -n +2 /var/lib/mysql-files/tables.txt)" > /var/lib/mysql-files/tables.txt
Without been echo
executed?
UPDATE
With my current code when /var/lib/mysql-files/tables.txt
does not exist I get tail: cannot open '/var/lib/mysql-files/tables.txt' for reading: No such file or directory
When the file exists I get something like
mysql -e "SHOW TABLES FROM reaction_production" > /var/lib/mysql-files/tables.txt
echo "table1
table2
table3" > /var/lib/mysql-files/tables.txt
Script2 must be evaluated when executed in the future, not when written now.
Thanks
Upvotes: 0
Views: 49
Reputation: 1888
You need to scape the $
on the process substitution so that it is treated as a string
in the current script and thus, evaluated when executing script2.sh
.
#!/bin/bash
cat > /etc/script2.sh << EOF
#!/bin/bash
mysql -e "SHOW TABLES FROM $DATABASE_NAME" > /var/lib/mysql-files/tables.txt
echo "\$(tail -n +2 /var/lib/mysql-files/tables.txt)" > /var/lib/mysql-files/tables.txt
EOF
Upvotes: 1