Solved

Make an easy Unix script with a single command

Posted on 2011-03-21
7
491 Views
Last Modified: 2012-05-11
Hello,

I want to run a command to export calendars from a Mirapoint email server. Right now, I know how to run a command from the command line and pipe that to a text file. But, I need to do this 100 times. In DOS, I could just create a simple text file, give it a BAT extension, and then run multiple line entries. Is Unix just as easy? I know hardly anything about Unix.

How can I create a batch file in Unix? Please be specific and tell me the steps. I will be using Putty as my editor.

Thanks,
John
0
Comment
Question by:jhieb
7 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 250 total points
Comment Utility
Hi,

first of all, PuTTY is not an editor, it's a terminal emulator.
The most common editor in Unix is vi.

So open a PuTTY session to your Unix server and issue

vi myfirstscript

(There is no need for extensions in Unix, but they're not forbidden eiter).

Enter input mode by just typing "i"

then type the following lines:

#!/bin/sh
/path/to/calendar command with arguments
exit

Now type

<ESC>:wq

to save the file. <ESC> means "Hit the escape key".

Allow execution of your new script with

chmod +x myfirstscript

From now on you can run the script by just typing its name and hitting <Enter>.

The first line (#!/bin/sh) in your script designates the command interpreter to be used, by the way.

wmp
0
 
LVL 9

Assisted Solution

by:AriMc
AriMc earned 167 total points
Comment Utility
PuTTY isn't really an editor but a terminal emulator software. In Unix/Linux you can create a script (or batch file) with a text editor like this:

- Start your editor with the preferred script file name as argument, for example, if you
  use nano (a very common editor in Linux distributions):

    nano myscript.sh

- As the first line in your script, type:

    #!/bin/sh

  This will force the use of the Bourne shell

- Next type the lines that execute your commands

     /usr/bin/myexportcommand -option1 etc
     /usr/bin/myexportcommand -option2 etc
     ...

- Save the file, then give the script execution rights

    chmod +x myscript.sh

- Now you can run the script simply with the command

   myscript.sh

- In practical situations you should place the script in a directory that is included in your
  path environment variable so it will always be found regardless of you curent working
  directory. Alternatively you can start the script by typing in the complete path, ie.
  something like "/home/myhomedir/myscript.sh".


0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 250 total points
Comment Utility
What I forgot to mention:

It might be that your current directory (the "." in Unix terms) is not in yout search path for executables.

You will then have to type

./myfirstscript
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 20

Assisted Solution

by:flow01
flow01 earned 83 total points
Comment Utility
You can create a batch file :
create a simple text file with the commands you would like to run
save it on the unix   (for example as mybatch)

execute the batch by entering on the command line
. mybatch
or make the batch an executable file
chmod 700 mybatch
(the first position marks the permissions of the owner  : 7 means you are able to read, write and execute the  file)
(the second positions marks the permissions of other member in the same group as the owner
chmod 750 mybatch
(makes the batch executable for them but they can't modify  it)

once you made a file executable you can run the batch by entering on the command line
mybatch
 
 
0
 
LVL 1

Author Comment

by:jhieb
Comment Utility
woolmilkporc,

I am sorry to say but I don't understand:

<ESC>:wq

I am in VI and what do I have to do to save to file what I typed? Yes, I am slow at this lol and need it spelled out for me.

Thanks,
John
0
 
LVL 9

Assisted Solution

by:AriMc
AriMc earned 167 total points
Comment Utility
<ESC>:ws means: press the ESC-key, then colon ":", then W and finally Q.

That sequence will first take you to VI's command mode, then save the file and finally quit the program. VI isn't exactly the easiest text editor in Unix to start with. :)

0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 250 total points
Comment Utility
That's why I wrote >> <ESC> means "Hit the escape key". <<

- hit the escape key
- type :
- type wq
- hit the enter key

escape leaves input mode
: brings the cursor into the command line at the bottom
wq means write and quit
enter means execute.

wmp
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now