[colug-432] Gracefully Doing Nothing

jep200404 at columbus.rr.com jep200404 at columbus.rr.com
Mon Jan 30 13:33:17 EST 2017


On Mon, 30 Jan 2017 13:07:26 -0500, Jeff Frontz <jeff.frontz at gmail.com> wrote:

> On Mon, Jan 30, 2017 at 12:56 PM, <jep200404 at columbus.rr.com> wrote:
> 
> > How does one do nothing gracefully in a Bourne shell for loop?

> Is nullglob (a la "set -o nullglob") available ?

No, but nullglob was the clue that led to a solution.
Thanks!

not in Bourne shell:

    jep at colug:~/stuff $ cat ~/bin/backup
    #!/bin/sh

    dir=~/stuff
    prefix=`date --iso=ns`

    set -o nullglob

    cd "$dir"
    for f in [a-z]*; do
        mv "$f" "$prefix"-"$f"
    done
    jep at colug:~/stuff $ ll
    total 0
    jep at colug:~/stuff $ backup
    /home/pi/bin/backup: 6: set: Illegal option -o nullglob
    jep at colug:~/stuff $

not in bash:

    jep at colug:~/stuff $ cat ~/bin/backup
    #!/usr/bin/env bash

    dir=~/stuff
    prefix=`date --iso=ns`

    set -o nullglob

    cd "$dir"
    for f in [a-z]*; do
        mv "$f" "$prefix"-"$f"
    done
    jep at colug:~/stuff $ ll
    total 0
    jep at colug:~/stuff $ backup
    /home/pi/bin/backup: line 6: set: nullglob: invalid option name
    mv: cannot stat ‘[a-z]*’: No such file or directory
    jep at colug:~/stuff $ 

but with the nullglob clue,
searching the web led to shopt -s nullglob for bash.
shopt does not work for Bourne shell:

    jep at colug:~/stuff $ cat ~/bin/backup
    #!/bin/sh

    dir=~/stuff
    prefix=`date --iso=ns`

    shopt -s nullglob

    cd "$dir"
    for f in [a-z]*; do
        mv "$f" "$prefix"-"$f"
    done
    jep at colug:~/stuff $ ll
    total 0
    jep at colug:~/stuff $ backup
    /home/pi/bin/backup: 6: /home/pi/bin/backup: shopt: not found
    mv: cannot stat ‘[a-z]*’: No such file or directory
    jep at colug:~/stuff $

but it does work for bash:

    jep at colug:~/stuff $ cat ~/bin/backup
    #!/usr/bin/env bash

    dir=~/stuff
    prefix=`date --iso=ns`

    shopt -s nullglob

    cd "$dir"
    for f in [a-z]*; do
        mv "$f" "$prefix"-"$f"
    done
    jep at colug:~/stuff $ ll
    total 0
    jep at colug:~/stuff $ backup
    jep at colug:~/stuff $

That's good enough to use.
What is the graceful way of doing nothing in Bourne shell?




More information about the colug-432 mailing list