Results 1 to 7 of 7

Thread: Cat is executing commands

  1. #1
    Join Date
    Apr 2013
    Beans
    46

    Cat is executing commands

    When I try to cat a block and save it, some function are executed, like this one

    Code:
    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

    Code:
    #!/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.

  2. #2
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Cat is executing commands

    This might help...
    http://tldp.org/LDP/abs/html/commandsub.html

    Cat takes anything inside $(...) and expanding it.
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

  3. #3
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Cat is executing commands

    +1
    Just put a backslash before each dollar sign in the command to prevent expansion:
    Code:
    USER_ID=\$(
        mysql -u \$DB_USER -p\$DB_PASS -h\$DB_HOST \$DB_NAME -B -e \
            "SELECT id FROM users WHERE username='\$USER';"
    )
    Last edited by schragge; April 17th, 2013 at 07:37 PM.

  4. #4
    Join Date
    Apr 2013
    Beans
    46

    Re: Cat is executing commands

    Thank buddies, its working now!

  5. #5
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: Cat is executing commands

    -1

    cat has nothing to do with the SHELL substitutions.

    You are using a `here document'.
    Check out: http://mywiki.wooledge.org/HereDocument
    and
    Code:
    man bash | less +/"^ +Here Documents"

    If you want to avoid shell substitution, just quote any character in the delimiter word:
    Code:
    cat << \EOF > file
    ...
    EOF
    
    #or
    cat << E\OF > file
    ...
    
    EOF
    
    #or
    cat << 'EOF' > file
    ...
    EOF

  6. #6
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Cat is executing commands

    But the OP needs parameter expansion inside here-document. That was the point of using it in the first place.

  7. #7
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: Cat is executing commands

    Quote Originally Posted by schragge View Post
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •