Bash Script Examples

From HPC

Jump to: navigation, search

Contents

qsub - Basic job script

Create a script, for example named myscript

#!/bin/bash
#$ -N MyJobName
#$ -cwd -V
myprog datafile

You would then submit the script

qsub myscript

qsub - Basic job script with email notification when job completes

Replace me@lshtm.ac.uk with your email address

#!/bin/bash
#$ -N MYHPCJOB
#$ -M me@lshtm.ac.uk -m e
#$ -V -cwd
myprogram

Using dates in scripts

You can get the date command to provide with dates in various formats, inc day of the week, week number, day number. Please see the Date format options list for complete list.

Using date in a script

#!/bin/bash

DATE=`date +%d/%m/%y`
DAYWEEKNUM=`date +%u`
HOUR24=`date +%H`  

echo $DATE
echo $DAYWEEKNUM 
echo $HOUR24

For a date/time of 19:05 Wednedays 22nd October 2008 the script would produce:

22/10/08
3
19 

Date format options

      %%     a literal %
      %a     locale's abbreviated weekday name (e.g., Sun)
      %A     locale's full weekday name (e.g., Sunday)
      %b     locale's abbreviated month name (e.g., Jan)
      %B     locale's full month name (e.g., January)
      %c     locale's date and time (e.g., Thu Mar  3 23:05:25 2005)
      %C     century; like %Y, except omit last two digits (e.g., 21)
      %d     day of month (e.g, 01)
      %D     date; same as %m/%d/%y
      %e     day of month, space padded; same as %_d
      %F     full date; same as %Y-%m-%d
      %g     the last two digits of the year corresponding to the %V week number
      %G     the year corresponding to the %V week number
      %h     same as %b
      %H     hour (00..23)
      %I     hour (01..12)
      %j     day of year (001..366)
      %k     hour ( 0..23)
      %l     hour ( 1..12)
      %m     month (01..12)
      %M     minute (00..59)
      %n     a newline
      %N     nanoseconds (000000000..999999999)
      %p     locale's equivalent of either AM or PM; blank if not known
      %P     like %p, but lower case
      %r     locale's 12-hour clock time (e.g., 11:11:04 PM)
      %R     24-hour hour and minute; same as %H:%M
      %s     seconds since 1970-01-01 00:00:00 UTC
      %S     second (00..60)
      %t     a tab
      %T     time; same as %H:%M:%S
      %u     day of week (1..7); 1 is Monday
      %U     week number of year with Sunday as first day of week (00..53)
      %V     week number of year with Monday as first day of week (01..53)
      %w     day of week (0..6); 0 is Sunday
      %W     week number of year with Monday as first day of week (00..53)
      %x     locale's date representation (e.g., 12/31/99)
      %X     locale's time representation (e.g., 23:13:48)
      %y     last two digits of year (00..99)
      %Y     year

Looping/Repeating commands

#!/bin/bash

for((i=1; i<=4; i++));do
    echo $i
done

So to submit a 5 job's (job1, job2, job3, job4, job5) via qsub, you would do

#!/bin/bash

for((i=1; i<=4; i++));do
    qsub job$i
done
Personal tools