How to write a script/batch file?

Posted on 2005-04-22
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 95

    Expert Comment

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

    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 95

    Expert Comment

    by:Lee W, MVP
    Can you post the contents of the file?

    Author Comment

    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
    LVL 7

    Expert Comment

    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".
    LVL 38

    Accepted Solution

    > 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

    >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

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    After running Ubuntu some time, you will be asked to download updates for fixing bugs and security updates. All the packages you download replace the previous ones, except for the kernel, also called "linux-image". This is due to the fact that w…
    Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
    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.:
    Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now