Execute a statement after logging in to SSH - bash

I am writing a script in which i have to execute a set of statments after logging in via SSH. So i am using this command
ssh -t user#adf-svc-1001.do.e1.bboo.com $COMMAND "&& exec bash -l"
Now this works if i give COMMAND as any single line statement such as ls or mkdir, but its not working if i put the following in COMMAND
COMMAND= "if [ $B==$A ]
then
echo 'ERROR'
fi "
I tried putting \ at the end of each line,changed the indentation,put it into a single line but its throwing error
fi : command not found
syntax error near unexpected token `&&'

The first problem is the ssh statement itself. ssh command should be run like:
ssh -t user#adf-svc-1001.do.e1.bboo.com $COMMAND && exec bash -l
Second one is that COMMAND variable should be assigned as one line like:
COMMAND="if [ $B==$A ]; then echo 'ERROR'; fi;"
I hope this helps.

Related

Bash: Can't store command output into a variable when executing commands in SSH

My bash script is
#!/bin/bash
ssh <HOST_IP> << EOF
echo "here"
output=$(echo "here")
echo $output
EOF
When I ran it, I got:
here
meaning that the $output variable was empty. How can this be? I SSHed into the host manually and ran the same commands, and was able to get "here" in $output.

Shell script continue after failure

How do I write a shell script that continues execution even if a specific command failed, however I want to output as error later, I tried this:
#!/bin/bash
./node_modules/.bin/wdio wdio.conf.js --spec ./test/specs/login.test.js
rc=$?
echo "print here"
chown -R gitlab-runner /gpc_testes/
chown -R gitlab-runner /gpc_fontes/
exit $rc
However the script stops when the node modules command fails.
You could use
command_that_would_fail || command_failed=1
# More code and even more
.
.
if [ ${command_failed:-0} -eq 1 ]
then
echo "command_that_would_fail failed"
fi
Suppose name of the script is test.sh.
While executing the scripting, execute it with below command
./test.sh 2>>error.log
Error due bad commands won't appear on terminal but will be stored in file error.log which can be referred afterwards.

Syntax error when running bash script in Homestead Vagrant

I'm trying to run a script in Vagrant (vagrant ssh), however I am getting the error below:
tests/unit/runTests.sh: 4: tests/unit/runTests.sh: Syntax error: "(" unexpected
Command: sh tests/unit/runTests.sh
The script runs fine outside for vagrant and the only script info I've been able to find about vagrant are related to provisions, which is not what I'm doing in this case.
Script:
#!/bin/bash
i=0
fails=()
for d in tests/unit/*/ ; do
eval "vendor/bin/peridot -c tests/unit/peridot.php $d"
if [ $? -eq 1 ]
then
fails[$i]=$d
fi
wait
done
for ((j=0; j < ${#fails[#]}; j++)) do
echo "${fails[$j]}"
done
I've read about "(" syntax errors being related to dash, but I am using the suggestions that those posts had (#!/bin/bash).
Thanks for any suggestions.
Command: sh tests/unit/runTests.sh
It means that you (vagrant) is trying to run your script in 'sh', not in 'bash'. In this case, the first line (#!/bin/bash) is ignored and 'sh' interpret doesn't expect '(' character.

How do I check if a program is running in bash?

I have made a chat-script in bash and I want to check whether or not netcat is running.
I've tried pgrep but and it's working but it prints out an error in the terminal but it still keeps going like normal.
This is a part of that script:
function session()
{
echo -n "Port (default is 3333): "
read port
if [ -n "${port}" ]
then
clear
echo "Only 2 users can talk to each other simultaneously."
echo "To send a message, simply write and hit enter. Press Ctrl+C to quit."
nc -l -p ${port}
if [ pgrep "nc -l -p ${port}" ]
then
echo "${l_name} logged in to chat session"
else
clear
new
fi
else
echo "Invalid port!"
new
fi
}
Don't put prep inside [ ]. That doesn't run the prep command, it just treats the word pgrep as an argument to the test command.
You also need to use the -f option to make pgrep match the entire command line, not just the program name.
It should be
if pgrep -f "nc -l -p ${port}"
then
...
else
...
fi
Try to run script with a "-x" parameter. This shows each line that runs. Here is a description from man page:
-x : After expanding each simple command, for command, case
command, select command, or arithmetic for command, display the
expanded value of PS4, followed by the command and its expanded
arguments or associated word list.
Here is an example script:
#!/bin/bash
for i in $( ls ); do
echo item: $i
done
If you run with -x you can see each command running with a head of + sign:
$ bash -x list.sh
++ ls
+ for i in '$( ls )'
+ echo item: list.sh
item: list.sh

Strange bash errors when passing script to ssh

I'm trying to run a local script remotely that I'm calling with some other code. The basic formate is
ssh -i ${PemKey} ${User}#${URL} 'bash -s' -- < ${Command}
I get the error line 24: ${Command}: ambiguous redirect
Command is a string with the name of the script I want to run and its arguments. If I change the script to just print the command as
echo "ssh -i ${PemKey} ${User}#${URL} 'bash -s' -- < ${Command}"
and then run the command myself it works just fine.
I've tried putting the command in a temp variable and then call it that way, like:
TEMP="ssh -i ${PemKey} ${User}#${URL} 'bash -s' -- < ${Command}"
$TEMP
echo $TEMP
This results in No such file or directory. Again the echoed version of the command runs just fine at the command line.
Anyone know what I'm doing wrong here?
It seems that executing $TEMP doesn't work correctly, as the whole string 'bash -s' -- < ${Command} is given in argument to ssh. And in fact if you create a file called ${Command} on you remote host you will get an error bash: bash -s: command not found.
A solution is to uses eval like this :
eval $TEMP
This really does what it should.

Resources