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
Solved

Make an easy Unix script with a single command

Posted on 2011-03-21
7
496 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Remote desktop Ubuntu from Windows 10 5 90
error log using ftp 7 56
awk variable in printf 1 33
CentOS 7 Linux for HP DL380 G4 32Bits 7 46
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

792 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