Executing local script/command on remote server - linux

I have a command that I want to run on machine B from machine A. If I run the command on machine B locally, it works fine.
Here is the command:
for n in `find /data1/ -name 'ini*.ext'` ; do echo cp $n "`dirname $n `/` basename $n
.ext`"; done
From machine A, I issue this command
ssh user#machineB for n in `find /data1/ -name 'ini*jsem'` ; do echo cp $n "`dirname $n `/` basename $n .jsem`"; done
But I get error syntax error near unexpected token do
What is wrong? I think it has something to do with double quotes, single quotes, semi colon because executing command ssh user#machineB ls works fine. So not issue of authentication or something else.
Thanks

Put your code in a script, myScript.sh and execute it, just like ls: ssh user#machineB myScript.sh

Related

echo: command not found. in shell script

echo 'Working Space'
read dirname #directory
if [ -n "$dirname" ]
...
fi
for dir in *
do
newname=`echo $dir | tr "[a-z] [A-Z]" "[A-Z] [a-z]"`
mv $dir $newname
done
~
~
~
~
~
~
I'm new to shell script. I have some problem with command echo.
It's my school assignment and my assistant gave me the template code.
However the first line which contains command 'echo' he gave got me an error.
./utol.sh: line 1: echo: command not found
I checked path /bin whether it contains echo command, and simple command like 'echo hi' works well in my terminal, so I'm guessing this problem is not related with echo itself but other lines.
Can anybody help me? Thanks.

Using a for loop inside a ssh statement replaces variable with empty string

I have the following statement in a bash script:
ssh $host "cd /directory; for i in *$date.gz; do echo $i; done; exit"
I expect it to print the name of each file in the directory that ends with the date, and is a zip file. By ssh-ing to the host on the command line, and searching the directory, I find that there should be 5 such files. However, this script returns 5 blank lines. I checked if the $date variable was properly defined inside the quotes (it was). When I replaced $i with 'adf', the script printed
adf
adf
adf
adf
adf
So it is correctly filtering out those 5 files, but it is just not printing their names, and is replacing the $i in the statement with nothing (so that that line is just echo). Why is it doing this, and how can I make it print the filenames? The same thing happens when I run this line on the command line.
By double-quoting your command, the variable expansion occurs before the ssh call.
So when you call this command line:
ssh $host "cd /directory; for i in *$date.gz; do echo $i; done; exit"
It calls the ssh command with two arguments: $host and "cd /directory; for i in *$date.gz; do echo $i; done; exit"
The second argument picks the content of the date variable and the i variable when the string is built. But at this time, you do NOT have the correct value for i yet.
I think that escaping $i into \$i should solve your issue:
ssh $host "cd /directory; for i in *$date.gz; do echo \$i; done; exit"

iterating through ls output is not occurring in bash

I am trying to ls the directories and print them out but nothing is being displayed. I am able to SSH and execute the first pwd. However, anything within the the for loop has no output. I know for sure there are directories called event-test- because I've done so manually. I've manually entered the directory (/data/kafka/tmp/kafka-logs/) and ran this piece of code and the correct output appeared so I'm not sure why
manually entered has correct output:
for i in `ls | grep "event-test"`; do echo $i; done;
script:
for h in ${hosts[*]}; do
ssh -i trinity-prod-keypair.pem bc2-user#$h << EOF
sudo bash
cd /data/kafka/tmp/kafka-logs/
pwd
for i in `ls | grep "event-test-"`; do
pwd
echo $i;
done;
exit;
exit;
EOF
done
It is because
`ls | grep "event-test-"`
is executing on your localhost not on remote host. Besides parsing ls is error prone and not even needed. You can do:
for h in "${hosts[#]}"; do
ssh -t -t trinity-prod-keypair.pem bc2-user#$h <<'EOF'
sudo bash
cd /data/kafka/tmp/kafka-logs/
pwd
for i in *event-test-*; do
pwd
echo "$i"
done
exit
exit
EOF
done
When parsing ls it is good practice to do ls -1 to get a prettier list to parse. Additionally, when trying to find files named "event-test-" I would recommend the find command. Since I am not completely sure what you are attempting to do other than list the locations of these "event-test" files I'd recommend something more similar to the following:
for h in "${hosts[#]}"; do ssh trinity-prod-keypair.pem bc2-user#$h -t -t "find /data/kafka/tmp/kafka-logs/ -type f -name *event-test-*;" ; done
This will give you a pretty output of the full path to the file and the file name.
I hope this helps.

How to append a variable in SCP command in shell script?

Below is my shell script in which I am trying to append $element in my below scp call in the if statement block.
for element in ${x[$key]}; do # no quotes here
printf "%s\t%s\n" "$key" "$element"
if [ $key -eq 0 ]
then
scp david#machineB:/data/be_t1_snapshot/20131215/t1_$element_5.data /data01/primary/.
fi
done
But whenever I am running my above shell script, I always get like this -
scp david#machineB:/data/be_t1_snapshot/20131215/t1_.data No such file or directory
When I have taken a close look in the above error message, the above scp statement is not right as it should be -
scp david#machineB:/data/be_t1_snapshot/20131215/t1_0_5.data /data01/primary/.
The value of $element should get replaced with 0 but somehow my above appending logic is not working. Is there anything wrong I am doing in the way I am appending $element in my above scp command
try t1_${element}_5.data
scp david#machineB:/data/be_t1_snapshot/20131215/t1_${element}_5.data /data01/primary/.
when you use t1_$element_5.data, bash will replace $element_5 with its value,
you don't have $element_5 defined, so you are getting
t1_.data No such file or directory

directorylist in shell sh-Script

i have a sh-Script with:
#!/bin/sh
dirs=( $(find . -maxdepth 1 -type d -printf '%P\n') )
echo "There are ${#dirs[#]} dirs in the current path"
let i=1
for dir in "${dirs[#]}"; do
echo "$((i++)) $dir"
done
answer=2
echo "you selected ${dirs[$answer]}!"
But i got the error:
symfonymenu.sh: Syntax error: "(" unexpected (expecting "}")
its the line ...dirs=
I like to echo all available directories in a folder that user can select it in a prompt interface.
You use features from the bash shell, so you should execute the script in bash. Change the first line to:
#!/bin/bash
/bin/sh can be any POSIX-compatible shell, for example on Ubuntu it's dash.
That's a bash script so you should make sure that you're running it with bash. Call it as bash script.sh. Also you should start your index from 0 not 1: let i=0.

Resources