I've done a lot of windows command shell scripting but I'm a novice in linux. Is there some way I can script/automate the following commands so that all I have to do is (in windows terminology) "run a batch file" to do all this? Here are my steps, in order:
Any way to automate all or part of this? THANKS.
Yes. Once you know it, you'll find that scripting in Linux is a delight compared to Windows. Imagine the difference between (horrid) command prompt and (less horrid) PowerShell, then multiply that difference by ten, and that's how much nicer it is to script in Linux.
The term is "shell scripting", because the command processor (equivalent of Windows command prompt) is called the 'shell'. Just to make life a bit more fruity, there are various shells available for Linux. The most common are variants of sh
such as bash
and ash
. Some crazy people use csh
or tcsh
or others, though, which have annoyingly different syntax.
To create a 'shell script', i.e. a batch file, just create a text file with this line at the top:
#!/bin/bash
(or whatever the name of your shell is). Typically, the filename may end in .sh
or there may be no special file ending at all.
In the remaining lines of the file just list the commands you would like to run, as if you were typing them in at a normal Linux terminal.
Then assign 'execute' permissions to that file. From the command line, you would use chmod u+x filename.sh
. That means add eXexecute permissions for the current User.
The real fun comes when you start to learn all the different things you can do in the shell. For example there are neat tricks like this:
my-first-command && my-second-command # only runs my-second-command if my-first-command succeeded
or this:
my-background-command & # runs my-background-command in the background, so that you can get on with other things
There are also lots of terrific little Linux/UNIX programs which make it easy to connect other programs together - for example grep
, uniq
, xargs
.
Having explained how great all that is, though, I don't think that's actually what you need to do.
The shell script part of your example boils down to:
rm /logs/hr/DV/appserv/JEN*; ls /logs/hr/DV/appserv
I get the impression you want to log in from Windows automatically to run these commands.
I believe that in PuTTY, if it's connecting via SSH, you can give it a command to run. So here's what I'd do.
authorized_keys
within the .ssh
directory of the psoftXXX
user. You can literally copy and paste it; that's probably easier than doing anything fancy. Be aware that this directory and/or file may not exist, in which case you'll need to create them; if the file exists already then be sure to append the new key to the end of the file instead of overwriting it.psoftXXX
user.Finally, in the PuTTY settings you can probably specify the command-line given above. You might well need to specify it like this:
/bin/bash -c "rm /logs/hr/DV/appserv/JEN*; ls /logs/hr/DV/appserv"
Note that there's one stage I haven't automated, which is pressing 1 at the menu. That's because I suspect this menu is implemented by giving you a special default login shell which isn't /bin/bash
but is instead /something/somewhere/which/shows/a/menu
. I hope that if you specify an alternative command in PuTTY, that setting will be totally ignored and instead you'll get your script to run.
You might need to play around a bit. Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With