An Introduction to Shell Scripting

28
An Introduction to Shell Scripting Paul Brown [email protected]

Transcript of An Introduction to Shell Scripting

AnIntroductiontoShellScripting

PaulBrown

[email protected]

Whatistheshell?

•  AcommandlineuserinterfaceforUnix-like

operatingsystems.

•  Interactiveandscriptingmodes

WhatistheBashShell?

•  BourneAgainSHell,replacingtheolderBourneshellin1989

•  DefaultshellonmostLinuxsystemsand

MacOS

Features

•  Usercustomisable

•  Wildcardmatching

•  Re-direction

•  Commandsubstitution

•  Controlstructures

Whentousetheshell

•  Asawrapperforaworkflow

•  Whenlaunchingotherprocesses

•  Whendoinglotsoffilesystemaccess

•  Whenlowlevelaccesstohardwareisrequired

Whennottousetheshell

Shellscriptingisofmuchlessusewhenanyof

thefollowingarerequired

•  Complexcalculations

•  Agraphicaluserinterface

•  Anykindofdebuggingbeyondverybasic

Startingup

•  Oftenopenedviathegraphicaldesktop

•  Startupfilesarereadtoprovideusercustomisations,eg.bash_profile,.bashrc

Someusefulcommandspaulbrosmacbook:varpaulbrown$cd$HOMEpaulbrosmacbook:~paulbrown$pwd/Users/paulbrownpaulbrosmacbook:~paulbrown$ls-ltotal104240drwxr-xr-x2paulbrownstaff6419Jun2017Anacondadrwxr-xr-x29paulbrownstaff92830Nov2017Androiddrwx------+93paulbrownstaff29761Nov12:34Documents-rw-r--r--1paulbrownstaff822Feb2018README.md-rw-r--r--1paulbrownstaff014Jul2015mcmc.csvlrwxr-xr-x1paulbrownstaff2520Sep2016meme->/Users/paulbrown/meme4.11paulbrosmacbook:~paulbrown$chmod755Documentspaulbrosmacbook:~paulbrown$cp-rAndroidAndroid.backuppaulbrosmacbook:~paulbrown$rm–rAndroid

EnvironmentVariablesNero:~paulbrown$echo$PATH/bin:/usr/bin:/usr/sbin:/sbin:/usr/local/bin:~/binNero:~paulbrown$meme-bash:meme:commandnotfoundNero:~paulbrown$exportPATH=$PATH:/usr/local/meme/binNero:~paulbrown$echo$PATH/bin:/usr/bin:/usr/sbin:/sbin:/usr/local/bin:~/bin:/usr/local/meme/binNero:~paulbrown$memeUSAGE:

meme <dataset>[optionalarguments]

Morevariables•  Therearenovariabletypes

•  VARNAMEisareference

•  $VARNAMEisthevalueheldthereNero:~paulbrown$echoPATHPATH•  Use${…}toaccesssubstrings

paul-browns-macbook:~paulbrown$STR="Helloworld"paul-browns-macbook:~paulbrown$echo${STR:6}Worldpaul-browns-macbook:~paulbrown$echo${STR/w/W}HelloWorld

Usingquotationmarks

•  Importanttoknowthedifferencebetweensingleanddoublequotes

•  Expressionsareevaluatedinside“…”,butnotinside‘…’paul-browns-macbook:~paulbrown$NAME="Paul"paul-browns-macbook:~paulbrown$echo"Hello$NAME"HelloPAULpaul-browns-macbook:~paulbrown$echo'Hello$NAME'Hello$NAME

Arrays

•  Declaringanarrayfruits=('Apple''Banana''Orange’)•  Accessingelementsecho${fruits[0]}

Readingfiles

cat,head,tail,morenero:~paulbrown$grep“paulbrown”/var/log/secure……Nov423:10:33nerosshd[44146]:pam_unix(sshd:session):sessionopenedforuserpaulbrownby(uid=0)

Writingfiles

•  Anumberofinteractivetexteditors,egvi,

nano

•  Alsousere-direction>,>>

•  echo“somecontent”>>script.sh

Redirection

•  Inputtoandoutputfromcommandcanbere-

directedawayfromstdinandstdout

•  Re-directoutputtofilels–l>dircontent.txt

Re-directinputfromfile

sort–k5–n<dircontent.txt

Redirection

Pipesareusedtochaincommandstogetherso

theoutputofonebecomestheinputofthenext

tail–n1000logfile.log|sort|more

ls–l|sort–k5–n

Commandsubstitution

•  Thisallowstheoutputofacommandtobecaptured

andusedpipedbacktobeusedasanargumentfor

somethingelse,ortobecapturedinavariable

•  Preferredwayistouse$(…)

rm–f$(find.–name“*.txt”)

ArithmeticexpansionUsecommandsubstitutionpaul-browns-macbook:~paulbrown$echo2+32+3paul-browns-macbook:~paulbrown$echo$((2+3))5paul-browns-macbook:~paulbrown$echo$(2+3)-bash:2+3:commandnotfoundpaul-browns-macbook:~paulbrown$leta=$((2+3))paul-browns-macbook:~paulbrown$echo$a5Bashhandlesonlyintegertypes.Usebctoperformcalculationswithfloatingpointtypes

paul-browns-macbook:~paulbrown$echo'scale=3;4/3'|bc1.333

RemoteShells

•  rsh(remoteshell).Donotuse,insecure

•  ssh(secureshell,port22)

paul-browns-macbook:~paulbrown$sshnero.wsbc.warwick.ac.ukpaulbrown@nero.wsbc.warwick.ac.uk'spassword:Lastlogin:MonNov423:10:342019from95.149.133.253-sh-4.1$hostnamenero.wsbc.warwick.ac.uk•  Alsosftpandscpscp/local/[email protected]:/home/paulbrown

Shellscripting

•  Conventionally,fileshave.shextension

•  Remembertosetexecutepermission

•  Scriptbeginswith

#!/bin/bash

Inputarguments

•  Referredtoas$1,$2etc..

•  $#isthenumberofinputs

•  Sameappliestofunctions

•  Usereadtorequestuserinput

Conditionals

•  Surroundanexpressionwith[[…]]

•  Stringoperators:-z,-n,==,!=,<,>,=~

•  Numericaloperators:-eq,-ne,-lt,-le,-gt,-ge

•  Fileoperators:-e,-f,-d,-r,-w,-x

Conditionals

#!/bin/bashif[[$#-lt3]];then

echo“Notenoughinputarguments”exit0

elif[[$#-gt5]];thenecho“Toomanyinputarguments”exit0

elseecho“OK”

fi

Conditionals•  Canbechainedtogetherusinglogicaloperators&&,||#!/bin/bashif[[$#-lt3]]||[[$#-gt5]];then

echo“Wrongnumberofinputarguments”exit0

elseecho“OK”

fi•  Theseoperatorsallowconditionalexecutionmkdirnewdir||echo“Cannotcreatedirectory”mkdirnewdir&&touchnewdir/newfile

WhileLoops

whilereadline;dofields=(${line})#expandtoarray…

done<infilebreakandcontinuecanbeusedwithintheloopbody

ForloopsAforloopiteratesaseriesofwordsinastringforiin$(ls);do

echo$idoneAC-styleforloopcanbecreatedusingarithmeticexpressionsfor((i=0;i<100;i++));do

echo$1doneRangeexpressionforiin{1..10};do

echo$1done

Functions

myFunc(){

locallocalVar=“Hello”$1;

echolocalVar;

}

myFunc“Paul”

Returnvaluescanbecapturedbycommandsubstitution

Gettinghelp

•  manpagesformostcommands

•  Hugeamountononlineresources,egagood

cheatsheetathttps://devhints.io/bash