Solved

Make an easy Unix script with a single command

Posted on 2011-03-21
7
495 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
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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Penetration Testing home based work 3 75
centos linux 65 126
awk sed variable in file 3 37
awk variable in printf 1 24
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

770 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