My first post on the Forums! I've just whipped up a quick script to slightly automate a log file retrieval process for work and to be completely honest my BASH scirpting knownledge is extremely limited as is my understanding of a lot of linux commands.
Basically the scripts purpose is to remove the hassle of having to type in several scp commands to retrieve logs from numerous boxes.
Fair warning the script could be completely laughable but this is why I'm asking for critique. Another massive no no is not having actually tested the script (this is why I'm asking the community to point out any majorily flawed coding).
Thank you in advance!
Things i'm not sure on;
- Using the read command purely as a pause process, will it work the way I intend it too?
- Will the individual scp commands process, prompt for password, process the transfer and then move onto the next scp command?
- If the script is run from within the USB directory will the cd and unmounting run into problems?
Your uppercase ECHO won't work. Commands in Unix are case sensitive, and the ECHO command just isn't known.
The dollar sign in front of the read command will throw an error. What's the intent behind that?
read won't wait for any key, but only continue to run on newline (eg. Enter)
Instead of backticks (`cmd`) start using $(cmd), as the former is considered deprecated.
A cd / will always succeed, so you don't have to rely on the $home variable being set
Instead of repeatedly calling date, why not just call it once, save the result in a variable, and use that from thereon?
And of course the script could be made shorter by using loops, but that's more or less optical for such a short script.
If you want to do a quick check of the syntax run bash -n script, which will do a syntax check but not run the script. Once it does that, you can run bash -x script to see a trace of what is done
Thanks for your input, I've made adjustments to the script as suggested. I'm going to learn about looping to see if I can optimise the script a bit more.
The $ behind read was from what I found on a website discussing it's purpose, I did think it was a bit strange.
I'll run the checks now, if I've made a mistake, I'd appreciate it being pointed out!
One thing I missed before: you should quote anything that you echo, since the text may contain special characters that the shell might try to interpret (for example the braces on line 14).
An (slightly advanced) example for a loop that you could use:
Hi
I need a advice for writing simple bash script,
I have a file pod.txt which contains source location and remote location:
/mnt/infile/20141103/701_0001.png/remote/tmp/pk21730/p0330223723074.png
/mnt/infile/20141103/203_0001.png/remote/tmp/pk21731/p0330223723081.png
and I must copy ... (6 Replies)
I am using RedHat 4.4 Version Operating System.
I want to copy one file from source to destination.I don't want to give a password when I am executing the below shell script.
code:
#!/bin/bash
scp -r file.zip 192.168.1.5:/root/
Here it asking a password to enter,Is there any way give a... (2 Replies)
Hi friends,
i need to prepare a script ( in perl)
i have a file called "demo.exe" in my local unix host.
i have a list of remote hosts in a file "hosts.txt"
now i need to push "demo.exe" file to all the hosts in "hosts.txt" file.
for this i need to prepare a script(in perl, but shell... (5 Replies)
Hi All,
I need script to perform below task.
1. I have a script in one server and need to copy this script to remote server
2. login in to remote server
3. run the script which i copied to this server.
#!/bin/bash
read a
scp /tmp/script.sh user@hostname:/tmp
ssh user@$a
./scirpt.sh
... (2 Replies)
I'm looking to use rsync to compare remote files and to copy the diff to a local directory, rather than transfer over the net.
The net connection is not fast enough to transfer these files (~1.8TB) and I'd like to sneakernet them instead.
Possible? (4 Replies)
Hey guys.
I'm knocking up a script, a part of which will be copying some pretty large files to some remote servers. With the size of the files I need to make sure that they don't lose any data / corrupt on the way (it's unlikely, but these are business critical).
Obviously I could use the... (2 Replies)
Hey Unix Gurus,
I'm having trouble in copying a file on 5 different servers, first how can you do it locally (i.e without the need to ssh to the server you want to copy the file) and if you need to ssh how do u run a command within that server. Please see my code below(it doesn't work somehow).... (10 Replies)
hi,
i want copy one or group of file from a computer to others, but i have some problem that do not allow me to do this.
i do this by scp command like this :
scp <file name> root@cpName:destinationAddress
but the problem is that it do not it automatically. it means when it is connecting to... (4 Replies)
I want to copy dump generated from oracle database to my local DAT drive. Currently I am copying remote file to local drive thru rcp command and later copy it to local DAT. Pls. help me in this.
Thanks
Man Mohan
email address removed (8 Replies)
I facing a problem with Unix command "rcp". I unable to perform a rcp between host machines. I have religiously followed the man pages, but still unable to solve the problem. Do i check for anything to perform this command? Pls help....thanks =) (3 Replies)