Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I disable shell globbing in a script?

2 views
Skip to first unread message

RolandRB

unread,
Jan 11, 2005, 8:17:59 AM1/11/05
to
I want to call a script in this form:

script t*

...but I do not want the t* to be expanded out into a list of file
names and nor do I want people to put the t* in quotes such as 't*'.
Can I set options in first line in the script to disable shell globbing
or a later line or will it be done no matter what?

Stephane CHAZELAS

unread,
Jan 11, 2005, 8:26:55 AM1/11/05
to
2005-01-11, 05:17(-08), RolandRB:

Issue a "set -f" to disable filename generation. That will
disable it once and for all. If you want to disable it only for
the "script" command, then you can only do it with the zsh
shell:

alias 'script=noblog script'

But, in the first place, why don't you want the shell to expand
that glob pattern?

--
Stephane

RolandRB

unread,
Jan 11, 2005, 8:59:34 AM1/11/05
to

Stephane CHAZELAS wrote:
> 2005-01-11, 05:17(-08), RolandRB:
> > I want to call a script in this form:
> >
> > script t*
> >
> > ...but I do not want the t* to be expanded out into a list of file
> > names and nor do I want people to put the t* in quotes such as
't*'.
> > Can I set options in first line in the script to disable shell
globbing
> > or a later line or will it be done no matter what?
>
> Issue a "set -f" to disable filename generation. That will

This is a script that will be used by people who know little about
Unix/Linux and know nothing about shell globbing. I don't want them
setting shell options like that.

> disable it once and for all. If you want to disable it only for
> the "script" command, then you can only do it with the zsh
> shell:
>
> alias 'script=noblog script'

I don't want them setting an alias like that either.

> But, in the first place, why don't you want the shell to expand
> that glob pattern?

Because I will be using it in a case statement as t* or whatever they
typed in. It will be used to select files out of a list and not files
in the current directory.

> --
> Stephane

Stephane CHAZELAS

unread,
Jan 11, 2005, 9:25:56 AM1/11/05
to
2005-01-11, 05:59(-08), RolandRB:
[...]

>> Issue a "set -f" to disable filename generation. That will
>
> This is a script that will be used by people who know little about
> Unix/Linux and know nothing about shell globbing. I don't want them
> setting shell options like that.
>
>> disable it once and for all. If you want to disable it only for
>> the "script" command, then you can only do it with the zsh
>> shell:
>>
>> alias 'script=noblog script'
>
> I don't want them setting an alias like that either.
[...]

Then you're stuck.

The best you can do if you can only modify the script (don't
call your script "script" as it is the name of a standard
command) is (inside the script) to have a look at the last line
of the user's shell history file (~/.bash_history) to see what
shell command line was used and this can't be done reliably.

--
Stephane

RolandRB

unread,
Jan 11, 2005, 10:38:29 AM1/11/05
to

Stephane CHAZELAS wrote:
> 2005-01-11, 05:59(-08), RolandRB:
> [...]
> >> Issue a "set -f" to disable filename generation. That will
> >
> > This is a script that will be used by people who know little about
> > Unix/Linux and know nothing about shell globbing. I don't want them
> > setting shell options like that.
> >
> >> disable it once and for all. If you want to disable it only for
> >> the "script" command, then you can only do it with the zsh
> >> shell:
> >>
> >> alias 'script=noblog script'
> >
> > I don't want them setting an alias like that either.
> [...]
>
> Then you're stuck.

I thought so :o(

> The best you can do if you can only modify the script (don't
> call your script "script" as it is the name of a standard
> command) is (inside the script) to have a look at the last line
> of the user's shell history file (~/.bash_history) to see what
> shell command line was used and this can't be done reliably.
>
> --
> Stephane


Then I'll have to use an option in the script and prompt for a file
pattern and use that in the case statement.

#!/bin/bash -f
# Script : seldone
# Version : 1.0
# Author : Roland Rashleigh-Berry
# Date : 11-Jan-2005
# Purpose : To select files from donelist.txt
# SubScripts : none
# Notes : none
# Usage : seldone
# seldone -s
#======================================================================
# OPTIONS:
#-opt- -------------------------------description----------------------
# s (switch) selection pattern
#======================================================================
# PARAMETERS:
#-pos- -------------------------------description----------------------
# N/A Do not supply parameters
#======================================================================
# AMENDMENT HISTORY:
# init --date-- mod-id ----------------------description---------------
#
#======================================================================

# Usage message and exit
usage () {
echo "Usage: seldone -s" 1>&2
exit 1
}


# Default is not to prompt for a file pattern
switchs=0


# "case" statement for action to take on selected options
while getopts "s" param ; do
case $param in
"?") # bad option supplied
usage
;;
"s") # (switch) selection pattern
switchs=1
;;
esac
done


# shift to bring first parameter to position 1
shift $(($OPTIND - 1))


# No parameters allowed
if [ $# -gt 0 ] ; then
echo "Error: (seldone) Do not supply any parameters" 1>&2
usage
fi


# if option set then prompt for file pattern else default to *
if [ "$switchs" == "1" ] ; then
echo -n "Enter your file pattern: "
read patt
else
patt=*
fi


# read in donelist.txt and select files that match the pattern
for filename in $(cat donelist.txt | cut -d' ' -f2-) ; do
case "$filename" in
$patt) echo "$filename"
;;
esac
done

CV

unread,
Jan 11, 2005, 12:17:46 PM1/11/05
to
RolandRB wrote:

I think the globbing actually happens before the script is called,
so whatever you do or set from within the script will be too late.

For any settings to have any effect (no, I can't tell you exactly
which ones) they would have to be set prior to calling the script,
in the environment from which it is called.

Cheers CV

Bill Seivert

unread,
Jan 12, 2005, 12:22:36 AM1/12/05
to

CV wrote:

The simplest solution is to use quotes (single or double) around the
parameter or
put a backslash (\) in front of the wildcard characters, i.e., script
t\*. But if there can
be multiple wildcards in the parameter, quotes are more effective.

Bill Seivert
sei...@pcisys.net

RolandRB

unread,
Jan 12, 2005, 3:03:56 AM1/12/05
to

I agree but the users of this script won't remember to do this.

Stephane CHAZELAS

unread,
Jan 12, 2005, 3:22:50 AM1/12/05
to
2005-01-12, 00:03(-08), RolandRB:
[...]

>> t\*. But if there can
>> be multiple wildcards in the parameter, quotes are more effective.
> I agree but the users of this script won't remember to do this.

Why don't you put

alias yourscript='noglob yourscript'

in /etc/zshrc?

If your users are not experienced Unix users, they would benefit
from having zsh as their login shell.

--
Stephane

RolandRB

unread,
Jan 12, 2005, 5:41:56 AM1/12/05
to

bash is the "official" shell here. I can't change it.

Stephane CHAZELAS

unread,
Jan 12, 2005, 5:57:42 AM1/12/05
to
2005-01-12, 02:41(-08), RolandRB:
[...]

>> in /etc/zshrc?
>>
>> If your users are not experienced Unix users, they would benefit
>> from having zsh as their login shell.
[...]

> bash is the "official" shell here. I can't change it.

yourscript() {
command yourscript "$@"
set +f
}
alias yourscript='set -f; yourscript'


in:

/etc/bash.bashrc
(your bash may not support system-wide bashrc as it's a compile
time configuration. If so consider using a PROMPT_COMMAND
env var in /etc/profile (not very reliable) or force every
~/.bashrc to source /etc/bash.bashrc)

may work. but beware of command lines like:
cmd1 || yourscript args

that won't work as you expect.

--
Stephane

Christian Schneider

unread,
Jan 12, 2005, 6:07:17 AM1/12/05
to
Thus spake RolandRB (rolan...@hotmail.com):

> Stephane CHAZELAS wrote:
>> 2005-01-12, 00:03(-08), RolandRB:
>> [...]
>> >> t\*. But if there can
>> >> be multiple wildcards in the parameter, quotes are more effective.
>> > I agree but the users of this script won't remember to do this.
>>
>> Why don't you put
>>
>> alias yourscript='noglob yourscript'
>>
>> in /etc/zshrc?
>>
>> If your users are not experienced Unix users, they would benefit
>> from having zsh as their login shell.
>
> bash is the "official" shell here. I can't change it.

You can. http://zsh.sunsite.dk/FAQ/zshfaq01.html#l8
--
#!/bin/sh -
set - `type $0` 'tr "[a-zA-Z]" "[n-za-mN-ZA-M]"';while [ "$2" != "" ];do \
shift;done; echo 'frq -a -rc '`echo "$0"| $1 `'>$UBZR/.`rpub signature|'`\
echo $1|$1`'`;rpub "Jr ner fvtangher bs obet. Erfvfgnapr vf shgvyr!"'|$1|sh

chrisb...@netscape.net

unread,
Jan 13, 2005, 11:58:38 AM1/13/05
to

If your users are naive about Unix, couldn't you get them to use a
different character than "*" for wildcard, eg "%"? Your script could
then bypass globbing (unless you have files in $PWD called *%* of
course) and process the list as you desire.

--
.

Bill Marcum

unread,
Jan 19, 2005, 11:32:40 PM1/19/05
to
On 12 Jan 2005 00:03:56 -0800, RolandRB
<rolan...@hotmail.com> wrote:
> I agree but the users of this script won't remember to do this.
>
If your users know so little about Unix, should they even be using a
shell prompt?

--
BOFH excuse #270:

Someone has messed up the kernel pointers

0 new messages