PDA

View Full Version : Cat is executing commands



pedrommone
April 17th, 2013, 07:01 PM
When I try to cat a block and save it, some function are executed, like this one


USER_ID=$(mysql -u $DB_USER -p$DB_PASS -h$DB_HOST $DB_NAME --disable-column-names -e "SELECT id FROM users WHERE username='$USER';")

My bash is the follow



#!/bin/bash


script="tunnel-bootstrap.sh"
temp=`getopt -l id:,name:,ip:,type:,host:,user:,pass:,db: -n "$0" -- '' "$@"` || exit 1
eval set -- optname optval "$temp"
declare -A opt
while shift 2; do
case $1 in
--id|--name|--ip|--type|--host|--user|--pass|--db) opt[${1#--}]="$2";;
esac
done
cat <<EOF >"$script"
#!/bin/bash


############################
# CARREGA DADOS DO SERVIDOR
############################


SERVER_ID=${opt[id]}
SERVER_NAME="${opt[name]}"
SERVER_IP="${opt[ip]}"
SERVER_TYPE="${opt[type]}"
DB_HOST="${opt[host]}"
DB_USER="${opt[user]}"
DB_PASS="${opt[pass]}"
DB_NAME="${opt[db]}"


############################
# INICIO DO CHECK-IN
############################


if [ $USER == "ubuntu" ]; then
return
fi


trap '' 2


USER_ID=$(mysql -u $DB_USER -p$DB_PASS -h$DB_HOST $DB_NAME --disable-column-names -e "SELECT id FROM users WHERE username='$USER';")
EOF


Thanks in advance.

CharlesA
April 17th, 2013, 07:07 PM
This might help...
http://tldp.org/LDP/abs/html/commandsub.html

Cat takes anything inside $(...) and expanding it.

schragge
April 17th, 2013, 07:30 PM
+1
Just put a backslash before each dollar sign in the command to prevent expansion:


USER_ID=\$(
mysql -u \$DB_USER -p\$DB_PASS -h\$DB_HOST \$DB_NAME -B -e \
"SELECT id FROM users WHERE username='\$USER';"
)

pedrommone
April 17th, 2013, 11:17 PM
Thank buddies, its working now!

sisco311
April 17th, 2013, 11:35 PM
-1 :)

cat has nothing to do with the SHELL substitutions. ;)

You are using a `here document'.
Check out: http://mywiki.wooledge.org/HereDocument
and

man bash | less +/"^ +Here Documents"


If you want to avoid shell substitution, just quote any character in the delimiter word:

cat << \EOF > file
...
EOF

#or
cat << E\OF > file
...

EOF

#or
cat << 'EOF' > file
...
EOF

schragge
April 17th, 2013, 11:43 PM
But the OP needs parameter expansion inside here-document. That was the point of using it in the first place.

sisco311
April 18th, 2013, 12:17 AM
But the OP needs parameter expansion inside here-document. That was the point of using it in the first place.

Oh, thanks, I missed that. Then yes, escaping the $ sings is the way to go.