12.07.2015 Views

Advanced Bash-Scripting Guide

Advanced Bash-Scripting Guide

Advanced Bash-Scripting Guide

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Advanced</strong> <strong>Bash</strong>-<strong>Scripting</strong> <strong>Guide</strong>#+ For example:# sh shft.sh a b c def 23 skidoountil [ -z "$1" ] # Until all parameters used up . . .doecho -n "$1 "shiftdoneecho# Extra line feed.exit 0# See also the echo-params.sh script for a "shiftless"#+ alternative method of stepping through the positional params.The shift command can take a numerical parameter indicating how many positions to shift.#!/bin/bash# shift-past.shshift 3 # Shift 3 positions.# n=3; shift $n# Has the same effect.echo "$1"exit 0# ======================== #$ sh shift-past.sh 1 2 3 4 54# However, as Eleni Fragkiadaki, points out,#+ attempting a 'shift' past the number of#+ positional parameters ($#) returns an exit status of 0,#+ and the positional parameters themselves do not change.# This means possibly getting stuck in an endless loop. . . .# For example:# until [ -z "$1" ]# do# echo -n "$1 "# shift 20 # If less than 20 pos params,# done #+ then loop never ends!## When in doubt, add a sanity check. . . .# shift 20 || break# ^^^^^^^^The shift command works in a similar fashion on parameters passed to a function. SeeExample 33-15.Chapter 4. Introduction to Variables and Parameters 36

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!