• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

Help regarding a shell script

Hi,

Below script is grabbing the  Sub directories from day before yesterday.

Can anyone  help me how to perform the same thing by logging into a different server (like ssh)


#!/bin/bash


cd  '/usr/HOME/TEST';

find *  -maxdepth 1 -mindepth 1  -type d   -ctime 2 >/usr/HOME/LOGS/out.txt ;

Open in new window

0
new_perl_user
Asked:
new_perl_user
  • 8
  • 7
1 Solution
 
xtermCommented:
Not sure if I'm reading your question correctly, but if you just want to run that same operation on a new server, then simply log in via ssh to the new server and type in line 4 above, hit enter, and then type in line 6 above, and hit enter.

Alternately, you can use a text editor to create a new file, and paste the contents of that script into the new file (let's call it myscript.sh)
Then you would need to make the file executable (chmod u+x myscript.sh)
Then you can just run it (./myscript.sh)

At any rate, let me know if you're asking something different that I'm missing, and I'll clarify
0
 
new_perl_userAuthor Commented:
Hi,

For example I am running this script on server x , now I want to run the script still on server x but what this script should do is ssh to server y  execute the find command and write to a log file.

I was trying this but it is not working..
 
#!/bin/bash

ssh servery@140.12.256.13

cd  '/usr/DATA/FILES';

find *  -maxdepth 1 -mindepth 1  -type d   -ctime 2 >/usr/HOME/LOGS/out.txt ;

Open in new window

0
 
xtermCommented:
Oh, I see.

Alright, if you want to write the log file on server y where you're running the command then its really easy:

ssh myuser@server_y "cd /usr/DATA/files;  find *  -maxdepth 1 -mindepth 1  -type d   -ctime 2 >/usr/HOME/LOGS/out.txt"

That will ssh as user myuser to host server_y and run the command specified, and put the output file in /usr/HOME/LOGS/out.txt on server_y

If you want the output to be written to server_x (the one you're coming from) then do:

ssh myuser@server_y "cd /usr/DATA/files;  find *  -maxdepth 1 -mindepth 1  -type d   -ctime 2" > /tmp/out.txt

Notice, the location of the quotes is the difference.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
new_perl_userAuthor Commented:
Hi,

I tried the below format and it is erring out as :

FIND: Parameter format not correct
0
 
xtermCommented:
Change the "*" to a "." - I just copied the command from your original post even though it looked wrong from the start.

0
 
xtermCommented:
Another question though, what operating system is server_y running?  If its not the GNU find (as you'd see on pretty much any Linux box) then it probably wouldn't know the maxdepth/mindepth options.
0
 
new_perl_userAuthor Commented:
Still the same error

ssh testbox@140.12.256.13 "cd /usr/HOME/FILES;
find .  -maxdepth 1 -mindepth 1  -type d   -ctime 2" >/usr/HOME/LOGS/out.txt ;
0
 
omarfaridCommented:
are you trying to collect the output on one server or you want it on the remote server? Also, do you want it to run without prompting for password?

For 2nd question, you can set trust between the two accounts on the two systems by exchanging ssh keys

for the 1st question, you may do

#!/bin/bash
ssh servery@140.12.256.13 "cd  /usr/DATA/FILES
find *  -maxdepth 1 -mindepth 1  -type d   -ctime 2 > /usr/HOME/LOGS/out.txt 2> /dev/null"
0
 
xtermCommented:
Please paste the output of "uname -a" from server_y.

BTW, the IP address you have in your command is invalid - you can't have a "256" in an IP address... not sure if that was just a typo posting here.
0
 
new_perl_userAuthor Commented:
I am collecting the output on the server where I am running the script not on the remote server where I am executing the find command.

Tried the above code and  the error was:

bash: line 1:/usr/HOME/LOGS/out.txt: No such file or directory
0
 
new_perl_userAuthor Commented:
The  above  ip address is just for an example
0
 
xtermCommented:
Use my original example from above and redirect it to /tmp/out.txt - the path /usr/HOME/LOGS obviously doesn't exist on your server.

So:

ssh testbox@server_y "cd /usr/DATA/files;  find .  -maxdepth 1 -mindepth 1  -type d   -ctime 2" > /tmp/out.txt

Then look in /tmp for your output file.
0
 
new_perl_userAuthor Commented:
I used the same above format. It is creating the Output file in tmp location . But no data is being written to it because it is showing an error as :

FIND: Parameter format not correct  

I changed from * to .
0
 
xtermCommented:
Please run "uname -a" on server_y and paste the output here - the remote server may have a different OS with a different version of the find command, if that is even possible - the output of your find error looks like the remote ssh server might be some kind of windows/cygwin server - is that accurate?
0
 
new_perl_userAuthor Commented:
cool for your hint it worked.
You  are right that server is a windows/cygwin kind of thing and it is trying windows find.

I changed the find from find  to /usr/bin/find and it worked.

Thanks all for the help.
0
 
xtermCommented:
awesome, you're welcome.  Enjoy.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now