Run .sh file terminal [duplicate] - shell

This question already has answers here:
Pass commands as input to another command (su, ssh, sh, etc)
(3 answers)
Difference between sh and Bash
(11 answers)
Closed last month.
I ran below command line from a terminal and it works:
sftp -i ~/.ssh/id_rsa username#host
put csv_file
put manifest_file
I want to automatically run this so I created a .sh file that looks like below:
#!/bin/bash
sftp -i ~/.ssh/id_rsa username#host
put csv_file
put manifest_file
and save it as run.sh. I ran it like below:
sh run.sh
It connect to the host but the next commands, the put lines, did not run.
Can I get insights why is that and how can I solve it?

It worked interactively because put commands were not consumed by shell but running sftp process. Script you have attempts to run put commands as standalone.
Redirection
To run put you can either feed them to standard input of the sftp:
#!/bin/bash
sftp -i ~/.ssh/id_rsa username#host <<END
put csv_file
put manifest_file
END
where <<END denotes take next lines until seeing END and redirect them (<<) to the standard input of sftp.
Batch File
Or alternatively if you can automate them via batch file called e.g. batch and containing simply the put lines:
put csv_file
put manifest_file
and then tell sftp it should process commands in batch file like this instead of having shell script:
sftp -i ~/.ssh/id_rsa username#host -b batch
NOTE: Since you have bash shebang in file you can make that file executable by chmod +x run.sh and then simply run it as ./run.sh.

Related

sshpass doesn't work in Bash but work outside on command line [duplicate]

This question already has an answer here:
Ansible inside script command not found
(1 answer)
Closed 3 years ago.
I am working on a script to get list of files from remote servers, each remote server has its own password, IP and user and path. That's why I am using sshpass to pass the password parameter dynamically. Here is my script to just get list of files for time being:
user='username'
pass='password' ->>> LOCATION_OF_REMOTE='hostpath'
ip='hostip'
path='hostpath'
data=`/usr/bin/sshpass -p $pass sftp -oBatchMode=no -b - -oStrictHostKeyChecking=no $user#$ip <<_EOF_
cd $path
ls
_EOF_`
echo $data >> list_of_files.txt
When I run this script I am getting error:
ERROR: sshpass: Failed to run command: No such file or directory
while exact same command works fine outside the script on command line. It just doesn't work when it's within the script. I tried to run this as root and non root use both. I don't know why it doesn't work from within the script.
I have a super silly mistake. By defining a variable "PATH", shell is considering it environment path and that's why I was getting that stupid error.

ssh can use in bash shell, can't use in shell file

I use ssh to login EC2(Amazon)
ssh -i /home/XXX/mykey.pem ec2-user#ec2-NN-NNN-NN-NN.us-foo-N.compute.amazonaws.com
It success,but when I write the command to the file like this:
login.sh
#! /bin/bash
ssh -i /home/XXX/mykey.pem ec2-user#ec2-NN-NNN-NN-NN.us-foo-N.compute.amazonaws.com
after chmod +x login.sh
I run the script, it return:
ssh: Could not resolve hostname
So how to solve it, thanks.
There are two likely reasons:
You have a typo in the hostname in the script.
The script has a CRLF instead of LF as the line ending on the ssh line. This often happens when you edit the file on a Windows system and transfer it to Unix. Use dos2unix to fix the script.

Using expect, spawn with scp

I'm currently trying to automate a file transfer using the scp command with a shell script and the expect package. Based on what I've seen it seems that I should have
#!/usr/bin/expect -f
But when I did that I still get the errors:
DirectoryChange.sh: line 33: spawn: command not found
couldn't read file "*Password:*": no such file or directory
DirectoryChange.sh: line 35: send: command not found
DirectoryChange.sh: line 36: interact: command not found
The code I have works something along these lines:
#!/usr/bin/expect -f
repository=$PWD"/subdirectory/"
set pass "***********"
cd $repository
spawn scp -r user#host:/copyDirectory/ .
expect "*Password:*"
send "${pass}\r";
interact
It's a bad practice to store passwords in scripts or any other file. Use SSH authentication keys instead.
Take a look at this tutorial.
Looks like you're invoking your expect script like sh DirectoryChange.sh. Clearly sh is not the correct interpreter for an expect script.
change the file extension: ".sh" is for shell scripts
make sure it has execute permissions then launch it with ./DirectoryChange.exp
repository=$PWD"/subdirectory/" is not how to assign variables in expect. remove this line and edit the cd line to cd subdirectory
you don't have to interact with scp, so change the last line to expect eof

bash command doesnt seem to work, but its echo does?

Well, I'm new to linux so this may be a very newbie kinda of thing, here it goes:
I have a script in which I'm trying to send some different jobs to remote computers (in fact Amazon's EC2 instances), these jobs are in fact the same function which I run with different parameters.
eventually in the script code I have this line:
nohup ssh -fqi key.pem ubuntu#${Instance_Id[idx]} $tmp
if I do:
echo nohup ssh -fqi key.pem ubuntu#${Instance_Id[idx]} $tmp
I get:
nohup ssh -fqi key.pem ubuntu#ec2-72-44-41-228.compute-1.amazonaws.com '(nohup ./Script.sh 11 1&)'
Now the weird thing. If I run the code with no echo in the script it doesnt work! it says in the nohup.out (in my laptop, no nohup.out is created in the remote instance) bash: (nohup ./Script.sh 10 1&): No such file or directory
The file does exist locally and remotely and is chmod +x.
If I simply run the very same script with an echo in front of the problematic line and copy its output and paste in the terminal, it works!.
Any clues welcome, thanks!
Try removing the single quotes from $tmp. It looks like bash is treating (nohup ./Script.sh 10 1&) as the command with no parameters, but technically nohup is the command with the parameters ./Script.sh 10 1.
The problem is the single quotes around the nohup command in your $tmp variable. These don't get used on the shell locally, so SSH passes them verbatim. This means remotely the ssh server tries to interpret (nohup ./Script.sh 10 1&) as a command (looks for a file named that) which there clearly isn't. Make sure you remove the single quotes in $tmp.

How do you execute a command on a remote system insde a BASH script?

As part of an intricate BASH script, I'd like to execute a command on a remote system from within the script itself.
Right now, I run the script which tailors files for the remote system and uploads them, then through a ssh login I execute a single command.
So for full marks:
How do I log into the remote system from the bash script (i.e. pass the credentials in non-interactively)?
How can I execute a command (specifically "chmod 755 /go && /go") from within the script?
Following Tim Post's answer:
Setup public keys and then you can do the following:
#!/bin/bash
ssh user#host "chmod 755 /go && /go"

Resources