Link to home
Start Free TrialLog in
Avatar of rohgan
rohgan

asked on

How to write a script/batch file?

I have 2 commands that i need to execute regularly on linux redhat. How can i save them to script file or seomthing that I can just execute and 2 commands will execute?
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

Create a text file with your favorite text editor.  Then put each command on a seperate line, just like you would with a batch file in DOS.

Then make sure the permissions are set so it can be executed.  Typically, you would set them to rwxr-xr-x, which can be done using the command:

chmod 755 batchfilename

(Note: there is no need to use extensions (.bat, .cmd, etc)).  You can if you like, but the key to it working is the execute (the x's in rwxr-xr-x).  See also:
http://www.computerhope.com/unix/uchmod.htm
Avatar of rohgan
rohgan

ASKER

I just get a bunch of syntax error and command not found when I try to execute it. When I type individual statements at prompt everything wrks
Can you post the contents of the file?
Avatar of rohgan

ASKER

Does not work even when it just contains:
setenv SIMTE_HOME /home/roh/simte

Error is:
setenv: command not found

It wrks when i just type "setenv SIMTE_HOME /home/roh/simte" at prompt
Actually, there's one other step involved... you need #!/bin/bash, #!/bin/tcsh, etc. at the start of the file, to indicate what shell interpreter should be used.  However, if you want setenv to be part of _your_ environment, you'll need to place it in a file such as .login or .profile (depends on your shell).  Some shells also allow you to set aliases, so you can run multiple commands at once, etc.; aliases are like macros, but for the shell.

E.g. a bash shell script might look like:

#!/bin/bash
echo "test 123"
echo $PATH
PATH="$PATH:/test"
export PATH
echo $PATH

You can actually place multiple commands in a single file, as suggested above, but you'd need to use the source command to run them.  E.g. "source test.sh".
ASKER CERTIFIED SOLUTION
Avatar of wesly_chen
wesly_chen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>Does not work even when it just contains:
>setenv SIMTE_HOME /home/roh/simte

>Error is:
>setenv: command not found

What are the commands you want to execute?

In general you can put them in the file, but it seems like your
script is being run by a different shell than you're using.

Try placing them in a file and then type...

. /path/to/file

OR

source filename

To execute the script in the current shell.

Another option would be to place #!/bin/csh         or #!/bin/tcsh
in the script so that it corresponds to the present shell.

Note however, that the 'setenv' command applies only to subshells;
as soon as the shell that 'setenv' was run in exits (like after the script
is done), the setenv goes away.

Now if you source the script that won't happen.

Also, if you want to permanently set an environment variable, your shell should
have an initialization file...  ~/.cshrc          for c shell   (/bin/csh)
        ~/.tcshrc   for tcsh shell  (/bin/tcsh)
~/.profile       for bourne shell/bourne again or korn shell (/bin/bash or /bin/sh
                       or /bin/pdksh  etc)

To see which shell you are using for login, try 'echo $0'   at the shell prompt
or      grep yourusername /etc/passwd