This example shell script 

  • shows the current directory and its contents
  • shows the path where the shell looks for commands and
  • compiles a small Fortran program using gfortran and executes it


#!/bin/bash
# 
# **************************** LICENSE START ***********************************
#
# Copyright 2021 ECMWF. This software is distributed under the terms
# of the Apache License version 2.0. In applying this license, ECMWF does not
# waive the privileges and immunities granted to it by virtue of its status as
# an Intergovernmental Organization or submit itself to any jurisdiction.
#
# ***************************** LICENSE END ************************************
#
# Firstjob         USER SERVICES  NOVEMBER 2021 - ECMWF 
#
#
#       This shell-script:
#
#               - shows the current directory and its contents
#               - shows the path where the shell looks for commands and
#               - where a Fortran program executes from
#
#
#       This shell script produces the standard output file 
#
#         Firstjob.<JOB-ID>.out
#
#       in the workdir directory (the user's $SCRATCH).
#
#
#-------------------------------
# setting options for SLURM
#-------------------------------
# Options that are specified within the script file should precede the
# first executable shell command in the file. 
# All options, which are case sensitive, are preceded by #SBATCH. 
# These lines become active only when the script is submitted
# using the "sbatch" command. 
# All job output is written to the workdir directory, by default. 

#SBATCH --qos=ef

        # Specifies that your job will run in the queue (Quality Of
        # Service) "ef". If this option is not specified, the job
        # would run in queue "nf".

#SBATCH --job-name=Firstjob

        # Assigns the specified name to the request

#SBATCH --output=Firstjob.%j.out

        # Specifies the name and location of STDOUT where %j is the job-id
        # The file will be # written in the workdir directory if it is a 
        # relative path. If not given, the default is slurm-%j.out in the
        # workdir.

#SBATCH --error=Firstjob.%j.out

        # Specifies the name and location of STDERR where %j is the job-id
        # The file will be # written in the workdir directory if it is a 
        # relative path. If not given, the default is slurm-%j.outin the
        # workdir.

#SBATCH --chdir=/scratch/...

        # Sets the working directory of the batch script before it is
        # executed.

#SBATCH --mail-type=FAIL
        
        # Specifies that an email should be sent in case the job fails.
        # Other options include BEGIN, END, REQUEUE and ALL (any state
        # change).

#SBATCH --time=00:05:00

        # Specifies that your job my run up to HH:MM:SS of wall clock
        # time. The job will be killed if it exceeds this limit. If
        # time is not defined, the default limit for the queue (qos)
        # will be used.


#-------------------------------
# print the current directory and its content
#-------------------------------

echo "
Please note that your job will be executed in the specified workdir:"

pwd     # prints the path name of the current directory 

echo "
List of the content of the current directory:
"  
ls -l

#-------------------------------
# show your PATH
#-------------------------------

echo "
This is your path:
"
echo $PATH 
echo "  
All the commands will be searched for in these directories
unless  you give an absolute pathname for the command."

#-------------------------------
# change to $SCRATCHDIR
#-------------------------------

echo "
You should run your program in the directory \$SCRATCH or \$SCRATCHDIR. 
All the files created in \$SCRATCHDIR will be deleted automatically
when the job terminates.
"
cd $SCRATCHDIR        
 
#-------------------------------
# create a Fortran 90 program
#-------------------------------

# cat is used to read the Fortran program from the input stream 
# and to write it to the file 'prog.f90'.
# cat reads line by line until it reaches a line which starts with EOF.

module load prgenv/gnu    # or euqivalently "module load pg"

cat > prog.f90 <<EOF
program prog

!
! print a string to stdout
!
  print *,'The FORTRAN program source file is called prog.f90' 
  print *,'The executable file is called a.out'

end program prog 
EOF

#------------------------------
# compile and link
#------------------------------

gfortran -march=native -O3 prog.f90 

# The compiler options -march=native -O3 provide some basic optimisation.

#------------------------------
# execution
#------------------------------

echo "\n\n"             # echoes 2 blank lines 

./a.out                 # ./a.out is the absolute pathname of the command 
                        # a.out; the '.' is replaced with the path of the
                        # current directory.
                        # If the current directory is given in your PATH,
                        # you can execute your program with 'a.out' instead 
                        # of './a.out'

#-------------------------------
# show the content of the current directory after execution of the program
#-------------------------------

echo "
The current directory \$SCRATCHDIR now contains:
" 
ls -l

#-------------------------------
# tidy up and terminate
#-------------------------------

echo "
In case you don't work in \$SCRATCHDIR the new files have to be deleted 
 manually if not needed any more, e.g. 
              /bin/rm -r filename "

 
exit 0          # terminate the script, returning 0 (default) as return code 
                # to the system 

#
# There is one output file produced by this job: 
#
#         Firstjob.<JOB-ID>.out
#
# in the workdir directory.
#
# End of example job 'Firstjob'