2. Shell and other kinds of programming

Shell programming: advanced parts of the bash tutorial

In [3]:
%%bash

NAME[0]="Zara"
NAME[1]="Qadir"
NAME[2]="Mahnaz"
NAME[3]="Ayan"
NAME[4]="Daisy"
echo "First Method: ${NAME[*]}"
echo "Second Method: ${NAME[@]}"
First Method: Zara Qadir Mahnaz Ayan Daisy
Second Method: Zara Qadir Mahnaz Ayan Daisy
In [14]:
%%file list.sh
#!/bin/sh

NAME[0]="Zara"
NAME[1]="Qadir"
NAME[2]="Mahnaz"
NAME[3]="Ayan"
NAME[4]="Daisy"
echo "First Method: ${NAME[*]}"
echo "Second Method: ${NAME[@]}"
Writing list.sh
In [15]:
%%bash
chmod 755 list.sh
./list.sh
First Method: Zara Qadir Mahnaz Ayan Daisy
Second Method: Zara Qadir Mahnaz Ayan Daisy
In [28]:
%%bash
echo "Hello, world!"

### These are all of the bash environment variables (LOTS OF LINES!):
#set

### These are the variables that have "TRIUMF" in their name:
set | grep TRIUMF

### Select a specific variable and parse the NAME=VALUE for VALUE:
set | grep TRIUMF_FONTS | cut -d= -f2

### instead of typing it, assign it to a variable:
FDIR=`set | grep TRIUMF_FONTS | cut -d= -f2`

echo "Fonts are in \"$FDIR\", look there"
Hello, world!
TRIUMF_FONTS=/usr/local/lib
TRIUMF_PLOTTER_TYPE=PSA
TRIUMF_TERMINAL_TYPE=X
/usr/local/lib
Fonts are in "/usr/local/lib", look there
In [46]:
%%bash
VARS=`set | grep TRIUMF`

for v in $VARS
do
  name=`echo $v | cut -d= -f1`
  value=`echo $v | cut -d= -f2`
  echo "Environment variable \"$name\" has a value \"$value\" "
done
Environment variable "TRIUMF_FONTS" has a value "/usr/local/lib" 
Environment variable "TRIUMF_PLOTTER_TYPE" has a value "PSA" 
Environment variable "TRIUMF_TERMINAL_TYPE" has a value "X" 
In [44]:
%%file hello.c
#include <stdio.h>
#include <math.h>

void main() {
    double f;
    
    f=sin(3.);
    printf("Hello, world!  sin(3)=%f\n",f);
}
Overwriting hello.c
In [45]:
%%bash
cc hello.c -lxxxx
./a.out
Hello, world!  sin(3)=0.141120

Homework

Use bash commands ar, nm, grep, find, cut, and bash loops as needed to find which of the *.a or *.so files in /usr/lib64 and/or in /lib and/or /lib64 and all of their subdirectories contains a subroutine to calculate arctan(). Ignore symbolic link files, just use the actual library file, to remove duplication.

In [51]:
%%file sol.sh

#!/bin/sh

# check input line for parameters, use the first
if [ $# == 0 ]
then
# at least one parameter is mandatory
  echo "  usage: sol.sh subroutine_name"
  exit
fi

# get here if at least one name was specified, use it

# if more than one, tell the user we are ignoring the extra input
if [ $# > 1 ]
then
# at least one parameter is mandatory
  echo "  warning: extra input \"$2 ...\" ignored"
fi

FILES=`find /usr/lib64 -name "*.a" -print`

for f in $FILES
do
#   echo "found: $f"
    ar -t $f | grep $1
done

FILES=`find /usr/lib64 -name "*.so.*" -print`

for f in $FILES
do
   result=`nm -D $f | grep "U $1"`
   if [ ! -z "$result" ] 
   then 
	echo "found $1 in: $f"
   fi
done
Overwriting sol.sh
In [50]:
%%bash
chmod 755 sol.sh
In [52]:
%%bash 
./sol.sh atan
  warning: extra input " ..." ignored
found atan in: /usr/lib64/libicui18n.so.50
found atan in: /usr/lib64/libicui18n.so.50.1.2
found atan in: /usr/lib64/liblcms2.so.2
found atan in: /usr/lib64/liblcms2.so.2.0.6
found atan in: /usr/lib64/sane/libsane-fujitsu.so.1
found atan in: /usr/lib64/sane/libsane-fujitsu.so.1.0.24
found atan in: /usr/lib64/sane/libsane-genesys.so.1
found atan in: /usr/lib64/sane/libsane-genesys.so.1.0.24
found atan in: /usr/lib64/sane/libsane-canon_dr.so.1
found atan in: /usr/lib64/sane/libsane-canon_dr.so.1.0.24
found atan in: /usr/lib64/sane/libsane-kvs1025.so.1
found atan in: /usr/lib64/sane/libsane-kvs1025.so.1.0.24
found atan in: /usr/lib64/libexslt.so.0.8.17
found atan in: /usr/lib64/libvorbis.so.0
found atan in: /usr/lib64/libvorbis.so.0.4.6
found atan in: /usr/lib64/libexslt.so.0
found atan in: /usr/lib64/libpixman-1.so.0
found atan in: /usr/lib64/libpixman-1.so.0.34.0
found atan in: /usr/lib64/libgfortran.so.3.0.0
found atan in: /usr/lib64/libodfgen-0.1.so.1
found atan in: /usr/lib64/libodfgen-0.1.so.1.0.4
found atan in: /usr/lib64/libart_lgpl_2.so.2
found atan in: /usr/lib64/libart_lgpl_2.so.2.3.21
found atan in: /usr/lib64/libgfortran.so.3
found atan in: /usr/lib64/libspeexdsp.so.1
found atan in: /usr/lib64/libspeexdsp.so.1.5.0
found atan in: /usr/lib64/libopus.so.0.3.0
found atan in: /usr/lib64/atlas/libsatlas.so.3
found atan in: /usr/lib64/atlas/libsatlas.so.3.10
found atan in: /usr/lib64/atlas/libtatlas.so.3
found atan in: /usr/lib64/atlas/libtatlas.so.3.10
found atan in: /usr/lib64/libopus.so.0
found atan in: /usr/lib64/libIlmImf.so.7
found atan in: /usr/lib64/libIlmImf.so.7.0.0
found atan in: /usr/lib64/libgsl.so.0
found atan in: /usr/lib64/libgsl.so.0.16.0
found atan in: /usr/lib64/libslang.so.2
found atan in: /usr/lib64/libslang.so.2.2.4
found atan in: /usr/lib64/liblapack.so.3
found atan in: /usr/lib64/libcdr-0.1.so.1.0.1
found atan in: /usr/lib64/liblapack.so.3.4
found atan in: /usr/lib64/liblapack.so.3.4.2
found atan in: /usr/lib64/libmspub-0.1.so.1
found atan in: /usr/lib64/libmspub-0.1.so.1.0.2
found atan in: /usr/lib64/libcdr-0.1.so.1
found atan in: /usr/lib64/libfreehand-0.1.so.1
found atan in: /usr/lib64/libfreehand-0.1.so.1.0.1
found atan in: /usr/lib64/libpython2.7.so.1.0
found atan in: /usr/lib64/python2.7/site-packages/numpy/.libs/libgfortran-ed201abd.so.3.0.0
found atan in: /usr/lib64/python2.7/site-packages/scipy/.libs/libgfortran-ed201abd.so.3.0.0
found atan in: /usr/lib64/libply-splash-core.so.2
found atan in: /usr/lib64/libply-splash-core.so.2.1.0
found atan in: /usr/lib64/libgd.so.2
found atan in: /usr/lib64/libgd.so.2.0.0
found atan in: /usr/lib64/libwmf-0.2.so.7.1.0
found atan in: /usr/lib64/libspiro.so.0
found atan in: /usr/lib64/libsox.so.2
found atan in: /usr/lib64/libsox.so.2.0.1
found atan in: /usr/lib64/libtiff.so.5
found atan in: /usr/lib64/libtiff.so.5.2.0
found atan in: /usr/lib64/libspiro.so.0.0.1
found atan in: /usr/lib64/libpoppler.so.46
found atan in: /usr/lib64/libpoppler.so.46.0.0
found atan in: /usr/lib64/libgs.so.9
found atan in: /usr/lib64/libgs.so.9.07
found atan in: /usr/lib64/libfprint.so.0
found atan in: /usr/lib64/libfprint.so.0.0.0
found atan in: /usr/lib64/libwmf-0.2.so.7
found atan in: /usr/lib64/libpangocairo-1.0.so.0
found atan in: /usr/lib64/libpangoxft-1.0.so.0
found atan in: /usr/lib64/libpangoxft-1.0.so.0.4000.4
found atan in: /usr/lib64/libgtk-x11-2.0.so.0
found atan in: /usr/lib64/libgtk-x11-2.0.so.0.2400.31
found atan in: /usr/lib64/libpangocairo-1.0.so.0.4000.4
found atan in: /usr/lib64/libcogl.so.20
found atan in: /usr/lib64/libcogl.so.20.4.2
found atan in: /usr/lib64/librsvg-2.so.2
found atan in: /usr/lib64/librsvg-2.so.2.40.16
found atan in: /usr/lib64/libMagickCore.so.5
found atan in: /usr/lib64/libMagickCore.so.5.0.0
found atan in: /usr/lib64/libgnomecanvas-2.so.0
found atan in: /usr/lib64/libgnomecanvas-2.so.0.3000.3
found atan in: /usr/lib64/libsane.so.1.0.24
found atan in: /usr/lib64/libfltk.so.1.3
found atan in: /usr/lib64/libsane.so.1
found atan in: /usr/lib64/tracker-1.0/libtracker-data.so.0
found atan in: /usr/lib64/tracker-1.0/libtracker-data.so.0.0.0
found atan in: /usr/lib64/libxatracker.so.2
found atan in: /usr/lib64/libxatracker.so.2.3.0
found atan in: /usr/lib64/libgeocode-glib.so.0
found atan in: /usr/lib64/libgeocode-glib.so.0.0.0
found atan in: /usr/lib64/libgdk-3.so.0
found atan in: /usr/lib64/libgdk-3.so.0.2200.10
found atan in: /usr/lib64/libgtk-3.so.0
found atan in: /usr/lib64/libgtk-3.so.0.2200.10
found atan in: /usr/lib64/libclutter-1.0.so.0
found atan in: /usr/lib64/webkit2gtk-4.0/libicui18n.so.57
found atan in: /usr/lib64/webkit2gtk-4.0/libicui18n.so.57.1
found atan in: /usr/lib64/libjavascriptcoregtk-4.0.so.18
found atan in: /usr/lib64/libgweather-3.so.6
found atan in: /usr/lib64/libclutter-1.0.so.0.2600.0
found atan in: /usr/lib64/libclutter-glx-1.0.so.0
found atan in: /usr/lib64/libjavascriptcoregtk-4.0.so.18.4.14
found atan in: /usr/lib64/libwebkit2gtk-4.0.so.37
found atan in: /usr/lib64/libwebkit2gtk-4.0.so.37.14.14
found atan in: /usr/lib64/libgweather-3.so.6.5.1
found atan in: /usr/lib64/libchamplain-0.12.so.0
found atan in: /usr/lib64/libchamplain-0.12.so.0.9.1
found atan in: /usr/lib64/libgnomekbdui.so.8
found atan in: /usr/lib64/libgnomekbdui.so.8.0.0
found atan in: /usr/lib64/liba2ps.so.1.0.0
found atan in: /usr/lib64/liba2ps.so.1
found atan in: /usr/lib64/qt-3.3/lib/libqt-mt.so.3
found atan in: /usr/lib64/qt-3.3/lib/libqt-mt.so.3.3
found atan in: /usr/lib64/qt-3.3/lib/libqt-mt.so.3.3.8
found atan in: /usr/lib64/libjavascriptcoregtk-3.0.so.0
found atan in: /usr/lib64/libjavascriptcoregtk-3.0.so.0.16.19
found atan in: /usr/lib64/libwebkitgtk-3.0.so.0
found atan in: /usr/lib64/libwebkitgtk-3.0.so.0.22.17
found atan in: /usr/lib64/libQtDeclarative.so.4
found atan in: /usr/lib64/libQtDeclarative.so.4.8
found atan in: /usr/lib64/libQtDeclarative.so.4.8.5
found atan in: /usr/lib64/libQtGui.so.4
found atan in: /usr/lib64/libQtGui.so.4.8
found atan in: /usr/lib64/libQtGui.so.4.8.5
found atan in: /usr/lib64/libQtSvg.so.4.8
found atan in: /usr/lib64/libQtSvg.so.4
found atan in: /usr/lib64/libQtCore.so.4
found atan in: /usr/lib64/libQtSvg.so.4.8.5
found atan in: /usr/lib64/libQtCore.so.4.8
found atan in: /usr/lib64/libQtCore.so.4.8.5
found atan in: /usr/lib64/libQtScript.so.4
found atan in: /usr/lib64/libQtScript.so.4.8
found atan in: /usr/lib64/libQtScript.so.4.8.5
found atan in: /usr/lib64/libmp3lame.so.0
found atan in: /usr/lib64/libmp3lame.so.0.0.0
found atan in: /usr/lib64/libavutil.so.54
found atan in: /usr/lib64/libavutil.so.54.31.100
found atan in: /usr/lib64/libschroedinger-1.0.so.0
found atan in: /usr/lib64/libschroedinger-1.0.so.0.11.0
found atan in: /usr/lib64/libopenal.so.1
found atan in: /usr/lib64/libopenal.so.1.16.0
found atan in: /usr/lib64/libtwolame.so.0
found atan in: /usr/lib64/libtwolame.so.0.0.0
found atan in: /usr/lib64/libsoxr.so.0
found atan in: /usr/lib64/libwx_gtk2u_core-2.8.so.0
found atan in: /usr/lib64/libsoxr.so.0.1.1
found atan in: /usr/lib64/libavcodec.so.56
found atan in: /usr/lib64/libavcodec.so.56.60.100
found atan in: /usr/lib64/libavfilter.so.5
found atan in: /usr/lib64/libavfilter.so.5.40.101
found atan in: /usr/lib64/libwx_gtk2u_ogl-2.8.so.0
found atan in: /usr/lib64/libwx_gtk2u_ogl-2.8.so.0.8.0
found atan in: /usr/lib64/libwx_gtk2u_core-2.8.so.0.8.0
found atan in: /usr/lib64/libwx_gtk2u_svg-2.8.so.0
found atan in: /usr/lib64/libwx_gtk2u_svg-2.8.so.0.8.0
found atan in: /usr/lib64/libruby.so.2.0
found atan in: /usr/lib64/libruby.so.2.0.0
found atan in: /usr/lib64/libkhtml.so.5
found atan in: /usr/lib64/libkhtml.so.5.14.8
found atan in: /usr/lib64/libkjs.so.4
found atan in: /usr/lib64/libkjs.so.4.14.8
found atan in: /usr/lib64/libpoppler.so.37
found atan in: /usr/lib64/libpoppler.so.37.0.0
found atan in: /usr/lib64/libQt5Gui.so.5.6
found atan in: /usr/lib64/libQt5Gui.so.5
found atan in: /usr/lib64/libQt5Gui.so.5.6.2
found atan in: /usr/lib64/libQt5Widgets.so.5.6.2
found atan in: /usr/lib64/libQt5Widgets.so.5
found atan in: /usr/lib64/libQt5Widgets.so.5.6
found atan in: /usr/lib64/libQt5Core.so.5
found atan in: /usr/lib64/libQt5Core.so.5.6
found atan in: /usr/lib64/libQt5Core.so.5.6.2
found atan in: /usr/lib64/libGraphicsMagick-Q16.so.3
found atan in: /usr/lib64/libGraphicsMagick-Q16.so.3.16.0
found atan in: /usr/lib64/octave/3.8.2/liboctave.so.2
found atan in: /usr/lib64/octave/3.8.2/liboctave.so.2.0.0
found atan in: /usr/lib64/octave/3.8.2/liboctinterp.so.2
found atan in: /usr/lib64/octave/3.8.2/liboctinterp.so.2.0.0
found atan in: /usr/lib64/libglpk.so.36
found atan in: /usr/lib64/libglpk.so.36.0.1
ar: /usr/lib64/libieee.a: File format not recognized
ar: /usr/lib64/libmcheck.a: File format not recognized
ar: /usr/lib64/libQtUiTools_debug.a: File format not recognized
nm: /usr/lib64/.libgcrypt.so.11.hmac: File format not recognized
nm: /usr/lib64/fipscheck/libgmp.so.10.2.0.hmac: File format not recognized
nm: /usr/lib64/fipscheck/libgmp.so.10.hmac: File format not recognized
nm: /usr/lib64/fipscheck/libfipscheck.so.1.2.1.hmac: File format not recognized
nm: /usr/lib64/fipscheck/libfipscheck.so.1.hmac: File format not recognized
nm: /usr/lib64/.libhogweed.so.2.5.hmac: File format not recognized
nm: /usr/lib64/.libhogweed.so.2.hmac: File format not recognized
nm: /usr/lib64/.libnettle.so.4.7.hmac: File format not recognized
nm: /usr/lib64/.libnettle.so.4.hmac: File format not recognized
nm: /usr/lib64/.libcrypto.so.10.hmac: File format not recognized
nm: /usr/lib64/.libssl.so.1.0.2k.hmac: File format not recognized
nm: /usr/lib64/.libssl.so.10.hmac: File format not recognized
nm: /usr/lib64/.libcrypto.so.1.0.2k.hmac: File format not recognized
nm: /usr/lib64/.libgnutls.so.28.43.0.hmac: File format not recognized
nm: /usr/lib64/.libgnutls.so.28.hmac: File format not recognized