Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


How to write a script/batch file?

Posted on 2005-04-22
Medium Priority
Last Modified: 2008-01-09
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?
Question by:rohgan
LVL 97

Expert Comment

by:Lee W, MVP
ID: 13847548
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:

Author Comment

ID: 13847622
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
LVL 97

Expert Comment

by:Lee W, MVP
ID: 13847633
Can you post the contents of the file?
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 13847669
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

Expert Comment

ID: 13848310
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:

echo "test 123"
echo $PATH
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".
LVL 38

Accepted Solution

wesly_chen earned 2000 total points
ID: 13848540
> setenv: command not found
"setenv" is csh/tcsh built-in command.
I think your current shell is not csh. Do "echo $shell" to check your current shell.
If you are writing csh shell script, then

--- script.csh ---

setenv SIMTE_HOME /home/roh/simte
<command one>  (i.e. /bin/cp file1 file2  ...)
<command two>
chmod +x script.csh
./script.csh   <=== to run that script
LVL 23

Expert Comment

ID: 13850829
>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


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

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In my business, I use the LTS (Long Term Support) versions of Linux. My workstations do real work, and so I rarely have the patience to deal with silly problems caused by an upgraded kernel that had experimental software on it to begin with from a r…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month11 days, 12 hours left to enroll

564 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