Assign a command to a variable - Help


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Assign a command to a variable - Help
# 1  
Old 05-14-2019
Assign a command to a variable - Help

Hi,

I have the script below. When i assign SSH_COMMAND to "ssh -o ConnectTimeout=2 ${SERVER} ${AS_SUDO} ${COMMANDS}" and then execute it as ${SSH_COMMAND} I get the following error:

Code:
ssh: Could not resolve hostname sudo: Name or service not known
ssh: Could not resolve hostname sudo: Name or service not known

But when I run the command directly from the script, it works as expected. The script below is running as expected. Can you guys help?


Code:
#!/bin/bash
#set -x


function usage {
    echo "USAGE: ${0} [-nsv] [-f FILE] COMMAND"
    echo "-f FILE   Use to override the default file"
    echo "-n        Use for dry Run"
    echo "-s        Use to run the command with sudo"
    echo "-v        Use for verbose"
}


SERVERFILE="/vagrant/servers"

if [[ ! -e "${SERVERFILE}" ]]; then
    echo "File ${SERVERFILE} does not exist"
    exit 0

fi

if [[ ${#} -lt 1 ]]; then
    usage
fi

if [[ ${UID}  -eq 0 ]]
then
echo "Do not execute this as root, use -s instead"
fi


while getopts 'nsvf:' opt
do
    case "${opt}" in
        n)
            DRY_RUN="true"   ;;
        s)
            AS_SUDO="sudo" ;;
        f)
            SERVERFILE=${OPTARG}  ;;
        v)
            echo "verbose mode"    ;;
    esac
done
shift "$(( OPTIND-1 ))"

COMMANDS=${*}
SSH_COMMAND="ssh -o ConnectTimeout=2 ${SERVER} ${AS_SUDO} ${COMMANDS}"

if [[ ! -z ${DRY_RUN} ]]
then
    echo "DRY RUN: ${SSH_COMMAND}"
else
    for SERVER in $(cat ${SERVERFILE})
    do
        #${SSH_COMMAND}
        ssh -o ConnectTimeout=2 ${SERVER} ${AS_SUDO} ${COMMANDS}
    done
fi

# 2  
Old 05-14-2019
Code:
ssh -o ConnectTimeout=2 ${SERVER} ${AS_SUDO} ${COMMANDS}

I think so
Code:
ssh -o ConnectTimeout=2 ${SERVER} "${AS_SUDO} ${COMMANDS}"

--- Post updated at 22:23 ---

No, the variable $SERVER is not set!

--- Post updated at 22:32 ---

Substitute and look at the output
Code:
echo ssh -o ConnectTimeout=2 $SERVER $AS_SUDO $COMMANDS

Stop escaping variables needlessly, please. Hard to read
# 3  
Old 05-14-2019
Thanks.

But in the for loop, I am using the SERVER var:

Code:
for SERVER in $(cat ${SERVERFILE})
    do
        #${SSH_COMMAND} ---> Not working
        #ssh -o ConnectTimeout=2 ${SERVER} ${AS_SUDO} ${COMMANDS} -----> Working
    done

What should I do to make it work just by using $SSH_COMMAND?
# 4  
Old 05-15-2019
true way
Code:
SSH_COMMAND="ssh -o ConnectTimeout=2 \$SERVER \$AS_SUDO \$COMMANDS"
...
eval $SSH_COMMAND

--- Post updated at 08:13 ---

or in single quotes
Code:
SSH_COMMAND='ssh -o ConnectTimeout=2 $SERVER $AS_SUDO $COMMANDS'
...
eval $SSH_COMMAND

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I assign the output of a command to a variable within a loop in bash?

In the else of the main if condition . else set lnk = $(readlink -f <path> | cut -d '/' -f7) echo "$lnk" if ] When I run the above on command line , the execution seems to be fine and I get the desired output. But when I try to assign it to a variable within a loop... (12 Replies)
Discussion started by: sankasu
12 Replies

2. UNIX for Beginners Questions & Answers

Need to pass variable in a command and assign value to a variable

Hello All, Hope you're doing well ! I am trying below command to be passed in a shell script, header_date_14 is a variable and $1 is the name of a file I intend to pass as a command line argument, however command line argument is not being accepted. header_date_14=$(m_dump... (8 Replies)
Discussion started by: ektubbe
8 Replies

3. Shell Programming and Scripting

Assign the result of a multiline command to a variable

Hi, I have the following command that lists all the .o files from all the directories except of vwin (which I don't want it) for i in `ls -d */*.o|awk '$0 !~ "vwin"'`; do echo $i; done The result is something like that dir1/file1.o dir1/file2.o dir2/file3.o etc. So, I want to create a... (9 Replies)
Discussion started by: scor6800
9 Replies

4. Shell Programming and Scripting

Unable to assign command output to variable

Code set -x STATUS="0" echo $STATUS for i in `ls -ltr Report*|awk '{ print $9 }'` do if then flg = "`head -1 "$i" |cut -c 31-33`" echo `head -1 "$i" |cut -c 31-33` echo $flg if then echo "having Fun" STATUS="2" else echo "no Fun" fi fi (2 Replies)
Discussion started by: Funkeydude
2 Replies

5. Shell Programming and Scripting

Assign command (with pipe) output to a variable

Hi , I would like to assign command (with pipe) output to a variable. The code is as follows. The goal of the code is to get the last folder folder with a particular name pattern. myDate=`ls | grep 2009 | tail -1` echo "myDate=" $myDate However, in the presence of the pipe, the code... (3 Replies)
Discussion started by: jeff_cen
3 Replies

6. UNIX for Dummies Questions & Answers

Cut Command value assign to variable

Hi, I am new to UNIX Scripting. I have been trying to use the CUT command to retrieve part of the header from a file and assign it to a variable. I have tried searching a lot, but I am still unsuccessful. Sample Header: HJAN BALANCE 20090616 I need to retrieve the date here, which always... (10 Replies)
Discussion started by: ragz_82
10 Replies

7. Shell Programming and Scripting

How to assign the result of a SQL command to more than one variable in shell script.

Hi Friends... Please assist me to assign the result of a SQL query that results two column, to two variables. Pls find the below code that I write for assigning one column to one variable. and please correct if anything wrong.. #! /bin/sh no=' sqlplus -s uname/password@DBname... (4 Replies)
Discussion started by: little_wonder
4 Replies

8. Shell Programming and Scripting

Assign bash command to variable

Hi I am trying to write a function that needs to be able to assign the last run shell command to a variable. The actual command string itself not the exit code of the command. I am using the bash command recall ability to do this as follows: alias pb='ps | grep ash' ... (3 Replies)
Discussion started by: Moxy
3 Replies

9. Shell Programming and Scripting

assign awk command result to a variable

#!/bin/sh # ## MYSTRING = `awk '/myApp.app/' /Users/$USER/Library/Preferences/loginwindow.plist` if then echo String not found defaults write /Users/$USER/Library/Preferences/loginwindow AutoLaunchedApplicationDictionary -dict-add -string Hide -bool YES -string Path -string... (9 Replies)
Discussion started by: dedmakar
9 Replies

10. Shell Programming and Scripting

assign subst|grep|sed command result to a variable

Hi, I'm quite new to scripting and I want to modify following line of an existing script: MYVAR=`subst |grep 'L:\\\:' | sed -e 's/.*\\\//'`; What I have to do is to use the content of a variable instead of the constant expression 'L:\\\:' as the grep string to be matched. Assuming I already... (5 Replies)
Discussion started by: snowbiker99
5 Replies
Login or Register to Ask a Question