write a bash script
I would like to make a bash script. I know a little about Python, but I have no experience with bash scripts.
I have a homework webpage. The students fill in the gaps or choose the right word. I managed to make a php file which saves their answers in a folder on my webhost. Works well. I can get the files from the webhost with ssh. Also works well. Step 1: Before I get the files I need to move any previously existing files from /home/pedro/winter2019-20/18BE/studiAnswers/ to /home/pedro/winter2019-20/18BE/oldstudiAnswers/ Step 2: Then run: sftp -i ~/.ssh/godaddy5_rsa -b ~/.ssh/goDaddy.sftpHW.batch myusername@123.456.789.123 Step 3: Maybe print "All done!" at the end. I have not yet mastered using ssh in Python and I think that is unnecessary. I think a straight up bash script would do. If I can add the path of the script to $PATHS, I could just call it any time. Any tips for this amateur, please? |
You could probably start with Part 4 of The Linux Command Line. If you are really starting out from the beginning, buying a paper copy might be useful since you need to have a good familiarity with at least some system utilities before delving into automating them with a script.
There are also: Greg's Bash Pitfalls Greg's Bash FAQ Greg's Bash Guide Bash is the default interactive shell on many systems and nearly all GNU/Linux systems. However there are others, like Zsh and Ksh. There is also plain POSIX sh. Most of the principles for Bash will apply to the others, just watch out for bash-only extensions. They can be useful but need to be used with awareness. |
In addition you can run remote commands via ssh
ssh -i my_key user@host "remote_command" |
Step 1 is a simple mv command:
Code:
mv /home/pedro/winter2019-20/18BE/studiAnswers/* /home/pedro/winter2019-20/18BE/oldstudiAnswers/ Quote:
|
Quote:
Either way, scp is considered deprecated as of OpenSSH 8.0 and should be avoided, especially in new endeavors. |
Thanks!
The reason I want this to work is just: up to now I use php to send the students' answers to an email, then use python to get the emails, check the answers and put the results in my timetable. I think this will be a bit quicker, that's all. And it is an alternative to email. My batch file is below. It works, to my amazement! Quote:
1. This is what I have written. Does it look OK? 2. I see many scripts have an ending .sh Is that necessary? I called it getHW.sh, put it in /home/pedro/myBashscripts/ Quote:
|
well, the OS and bash doesn't really care what a script ends with
But
use [code] instead of [quote] Quote:
if it were like bash Code:
mget * || echo error && rm * regards the script that moves to old, does it matter if it overwrites files? would it make sense to tar up a batch and archive instead of leaving loose ? Edit: I didn't dig deep, but found this in sftp manpage Quote:
so the trick would be to grab stdout ( or just stderr? ) work out what went wrong and act accordingly it might be that you get a nice exit code Code:
sftp ...... Code:
sftp .... 2>stderr.log > stdout.log |
Thank for the suggestions.
I will find a workaround. Good idea to hold up on the rm * until I've marked all the answers and see who "forgot", check if there is a file which was not downloaded, then delete. I have about 150 students. My python program only gets unread messages. Only after I have marked everything and checked do I run another little program to mark all the emails as seen, but, of course, the emails are still there. Maybe I can make a little php program to copy all the files to another directory on the server, then run my bash script. I just don't want this weeks HW getting mixed up with next weeks HW. The files are all just tiny text files, they don't take up much space. I only use about 5% of the webspace I have. Just had an idea: Back them up immediately! This is the bit of the php which writes the text files, I just added a second path and file. Each file is unique, because they are marked with the student number and week number like 1825010132_18BEwW4 I just doubled up! Code:
$path = "/home/myusername/public_html/18BE/php/uploads/"; |
Quote:
Code:
$path1="/home/myusername/public_html/18BE/php/uploads/"; |
Thanks again, I will definitely look into rsync. Maybe I can get it to work, I am not too good with this stuff!
Do these 2 need to go together, or are they completely separate commands? Quote:
Quote:
I would like to see the --progress. Is this right? Quote:
|
Quote:
same as Code:
ssh -i ~/.ssh/godaddy5_rsa username@$server "mv -av $path1 $path2" Quote:
this is what you want Code:
rsync -av --progress -e 'ssh -i ~/.ssh/godaddy5_rsa' "username@123.123.123.123/$path2" /some/local/directory/ If I understand it correctly you
Untested Code:
#!/bin/bash but I assume it wouldn't have any ( that is a lazy assumption , I should test rsync to find out how to protect spaces properly ) I don't know what your workflow is, here I archived them straight away leaving originals alone They would need to be move/removed before next script run |
Thank you all for your advice. I am nearly done!
I've been trying to make rsync work. I seem to have got the moving files on the server bit working. Quote:
Quote:
How to build the second line into the first line?? I've tried, but then it fails. |
Well,
(1) -- is moving the files on the remote server from one folder to another (2) -- is fetching files from the remove server, and then removing them I don't see any need for (1) |
Tip with ssh
~/.ssh/config Code:
Host exampleservername.com Host can be an alias Code:
#instead of |
Code:
Host exampleservername.com Edit2: fixed misformatted excerpt |
All times are GMT -5. The time now is 08:51 PM. |