Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to automate some linux commands

Tags:

linux

ksh

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:

  • launch putty, choose hostname & port, click Open (would love to script/automate this 1st part too)
  • linux shell/terminal opens
  • I enter my login and pwd
  • I enter this command: sudo su - psoftXXX
  • I enter my pwd again and hit enter
  • I am presented with a little cmd-shell menu and a prompt. I have to type "1" and hit enter
  • cd /
  • cd logs
  • cd hr
  • cd DV
  • cd appserv
  • rm JEN*
  • ls

Any way to automate all or part of this? THANKS.

like image 733
HerrimanCoder Avatar asked Jan 15 '23 05:01

HerrimanCoder


1 Answers

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.

Creating a script

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.

Your specific example

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.

  1. Generate ssh keys on your Windows machine using PuTTY. (I can't remember how to do this - I think there's PuTTYGen or similar).
  2. Copy the public key that results into a file called 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.
  3. Now try to connect again using PuTTY and ssh. It should automatically log in as the psoftXXX user.
  4. 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!

like image 71
Adrian Taylor Avatar answered Jan 16 '23 19:01

Adrian Taylor