• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 508
  • Last Modified:

Make an easy Unix script with a single command

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
jhieb
Asked:
jhieb
6 Solutions
 
woolmilkporcCommented:
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
 
AriMcCommented:
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
 
woolmilkporcCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
flow01Commented:
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
 
jhiebAuthor Commented:
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
 
AriMcCommented:
<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
 
woolmilkporcCommented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now