Solved

Make an easy Unix script with a single command

Posted on 2011-03-21
7
498 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 250 total points
ID: 35184997
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
ID: 35185009
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
ID: 35185015
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
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!

 
LVL 20

Assisted Solution

by:flow01
flow01 earned 83 total points
ID: 35185030
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
ID: 35194472
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
ID: 35194518
<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
ID: 35194525
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

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