Bash set up, scripts, and loops - anjavdl/PHY517_AST443 GitHub Wiki

General

The bash shell (your default command line environment on many systems) is in itself a pretty powerful programming language. A description is found here:
http://www.tldp.org/LDP/abs/html/

Setting up your bash shell on uhura/vulcan

You need to tell your bash environment where the various executables (and other files) are located. To do so, copy the following into your .bashrc file on uhura:

export PATH=/usr/local/conda27/conda27/bin:$PATH
export PATH=$PATH:/usr/local/ds9/saods9/bin
export PATH=$PATH:/usr/local/topcat/bin
export PATH=$PATH:/usr/local/astrometry/bin
export PATH=$PATH:/usr/local/sextractor/bin
export PATH=$PATH:/usr/local/dfits/bin

export HEADAS=/usr/local/heasarc/heasoft-6.22/x86_64-unknown-linux-gnu-libc2.17
. $HEADAS/headas-init.sh

export TEXINPUTS=.:/usr/local/latex/aastex_611/:$TEXINPUTS

After you have done this, type . ${HOME}/.bashrc (on the command line) for these changes to take effect for your current session. Alternatively, log out and log back in - the .bashrc file is loaded on start-up.

Note that the .bashrc files are not shared between the different computers in the lab. Therefore, if you are working on vulcan (or another machine), then you need to copy your .bashrc file from uhura to that computer.

Writing and running a bash script

Bash is very well suited to write programs which mainly call command-line tasks. To do so, write the tasks into a text file where the first line is #! /bin/bash -u. Save this file, let's say it's called script.sh, and make it executable:

chmod 755 script.sh

You can now execute the script by typing

./script.sh

Simple script examples can be found here.

Loops in bash

Bash makes it easy to execute the same operation on many files. For example, let's assume you have a text file images.dat which lists all the images on which you want to astrometry.net on. The following script will do precisely that:

#! /bin/bash -u

filename=$1

while read file
do
   solve-field [arguments] ${file}  # complete this line with your call to solve-field
done < ${filename}

Note that this script takes an argument, in this case the name of your input file. I.e. you would call it as

./script.sh images.dat

If the files you want to operate on have a distinctive name, for example image_1.fits, image_2.fits, ... , you can let bash find them for you:

#! /bin/bash -u

for file in $(ls -1 image_*.fits)
do
   solve-field [arguments] ${file}  # complete this line with your call to solve-field
done

Nohup

(This is technically not bash, but POSIX). Nohup stands for "no hang-up" and lets you disconnect a process from your current shell. Thus, you can use it to start a script/program that will take a long time to run e.g. on uhura, and then log out of uhura without the script stopping. This is useful e.g. for running astrometry.net on many files. To use it, you simply add nohup in front of the name of the script/program, and follow it with an ampersand:

nohup ./script.sh &

If you find that your process still terminates when you log out, enclose the whole command in parentheses:

(nohup ./script.sh &)