Skip to content

Latest commit

 

History

History

Example1

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Purpose:

This example illustrates the use of Bash scripts on the Harvard University FAS cluster. The specific example computes the integer sum from 1 to N, where N is a number read from the command line.

Contents:

  • sum.sh: BASH source code
  • run.sbatch: Batch-job submission script
  • bash_sum.out: Output file

BASH source code:

#!/bin/bash
#=================================================
# Program: sum.sh
#          Sum up integers from 1 to N, where N
#          is read from the command line
#
# Example usage: sh sum.sh 100
#=================================================
n=$1
k=0
for i in `seq 1 $n`
do
    k=$(($k+$i))
done
echo -e "Sum of integers from 1 to $n is $k."

Example Batch-Job Submission Script:

#!/bin/bash
#SBATCH -J bash_sum
#SBATCH -o bash_sum.out
#SBATCH -e bash_sum.err
#SBATCH -p shared
#SBATCH -c 1
#SBATCH -t 0-00:30
#SBATCH --mem=2G

# Run the program
sh sum.sh 100

Example Usage:

sbatch run.sbatch

Example Output:

$ cat bash_sum.out 
Sum of integers from 1 to 100 is 5050.