help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] opposite of 'shift'


From: Greg Wooledge
Subject: Re: [Help-bash] opposite of 'shift'
Date: Fri, 17 Feb 2012 08:15:24 -0500
User-agent: Mutt/1.4.2.3i

On Thu, Feb 16, 2012 at 05:16:08PM -0600, Peng Yu wrote:
> A fare comparison is that which of the following is better?
> 
> set -- xxx $@

You mean set -- xxx "$@"

The quotes are NOT optional there.

> or
> 
> unshift xxx
> 
> It is obvious that the 2nd case is better.

When would you actually USE this in a script?

A typical script that has options and filename arguments looks like this:

#!/bin/bash

verbose=0
outputfile=/dev/stdout

while [[ $1 = -* ]]; do
  case $1 in
    -v) verbose=1; shift;;
    -q) verbose=0; shift;;
    -f) outputfile=$2; shift 2;;
    --) shift; break;;
  esac
done

for filename; do
  process "$filename"
done

When would unshift be useful?  Show me a script, or give me a description
of a problem that a script would be better able to solve, if you could
unshift the positional parameters.

(Oh wait... didn't I already say I wouldn't bother asking you for that,
because I knew you wouldn't provide it?  Oops.  I guess I lied.)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]