globbing, $# is too high after wildcard expansion in bash script


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
globbing, $# is too high after wildcard expansion in bash script

How can I pass in an argument such as "*.k" to a bash script
without having to double-quote *.k and not having *.k
`glob` to match all files in the pattern?

I tried using noglob in my script but this didn't work the way I thought
it would.. expansion is still occuring, $# is higher than I expect.

Code:
set +o noglob

$# represents *.k pattern after expansion (I dont want that) plus
the other positional parameters that I'm passing in.

Code:
# Test argument count
USAGE="USAGE: $0 <vertical> <src module> <files(s)> "
#
echo $# $0 $1 $2 $3
if [ $# -ne 3 ]
then
   if [ $# -ne 1 -a $1 != "bdat" -a $1 != "cdat" -a $1 != "fdat" -a $1 != "blay" ]
   then
      echo $# $0 $1 $2 $3
      echo $USAGE ; exit 1
   fi
fi


TIA
Zoo591
# 2  
We had this discussion in a similar thread recently I think. There's no way you can avoid the globbing occurring within your script because it is performed by the calling shell. If you can live with turning off globbing in your normal shell (which I suspect is unlikely) then you can turn it off there before running your script.

A suggestion here was to use an alias to turn off globbing, however there is no easy way to turn it back on automagically:

UNIX Scripting - Turn off file expansion for command line arguments
# 3  
In https://www.unix.com/unix-advanced-ex...ing-set-f.html this was fixed by combining an alias and a shell function.
# 4  
Nice solution; only seems to work in bash as you surmised in the referenced thread, but that'll be good enough for the OP!
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #878
Difficulty: Medium
Memory allocation is less critical in a real-time operating system (RTOS) than in other operating systems.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash variable expansion in awk script

Hello, I need to split a file into two of different locations by re-direction in awk. cat infle aaa 1 3 bbb 2 4 aaa 3 3 bbb 4 4 aaa 5 3 bbb 6 4 cat /storage/tmp/group_a.gtf aaa 1 3 aaa 3 3 aaa 5 3 cat /storage/tmp/group_b.gtf bbb 2 4 bbb ... (2 Replies)
Discussion started by: yifangt
2 Replies

2. Shell Programming and Scripting

Terminal running bash/rsync script does not close with exit (MacOS High SIerra)

Hello, I am running a bash script to do an rsync back on a computer running MacOS High Sierra. This is the script I am using, #!/bin/bash # main backup location, trailing slash included backup_loc="/Volumes/Archive_Volume/00_macos_backup/" # generic backup function function backup {... (12 Replies)
Discussion started by: LMHmedchem
12 Replies

3. Shell Programming and Scripting

Bash expansion

Hello. I cannot write a command without using eval. Any help is welcome Note 1 : What does the function SOMETHING has no importance. Note 2 : What does the command find has no importance. It is an expansion variable problem : where to put or or or anythings else What works (FILTRE_1... (8 Replies)
Discussion started by: jcdole
8 Replies

4. Shell Programming and Scripting

Negation in Bash Globbing

$ ls -1 a.1 b.1 x_a.1 x_b.1 $ ls -1 * b.1 x_a.1 x_b.1 $ ls -1 ** a.1 b.1 x_a.1 x_b.1The last result is not as expected. Why? Thanks. (2 Replies)
Discussion started by: carloszhang
2 Replies

5. Shell Programming and Scripting

Wildcard in bash script

Hi there, I am pretty new to linux scripting so .. I am writing a script to loop through all my directories of sequence files in order to do stuff with them (trimming, normalizing, stuff that one would do with sequence files). Here I need to pick out files that match each other. The files... (10 Replies)
Discussion started by: jahndavik
10 Replies

6. Shell Programming and Scripting

Problem when concatinating wildcard onto file location in bash script

I am having difficulty with the following script: #! /bin/bash filelist=~/data/${1}* ~/./convertFile $filelist ~/temp/outputEssentially, there are a large number of files in the directory ~/data, each with a four-letter code at the beginning (eg. aaaa001 aaaa002 bbbb001 bbbb002 etc). The... (11 Replies)
Discussion started by: Lears_Fool
11 Replies

7. Shell Programming and Scripting

wildcard in bash shell script

I tried to use the wildcard '*' in my bash script, but I can not get it work. Here is a simple example (list file names in current directory): ls ./* does not work in my bash script. But it works if I use ls ./ So is there any special syntax to use '*' wildcard in bash script (I tested the... (11 Replies)
Discussion started by: aerosols
11 Replies

8. Shell Programming and Scripting

Picking high and low variables in a bash script - possible?

Is it possible to have a bash script pick the highest and lowest values of four variables? I've been googling for this but haven't come up with anything. I have a script that assigns variables ($c0, $c1, $c2, and $c3) based on the coretemps from grep/sed statements of sensors. I'd like to also... (5 Replies)
Discussion started by: graysky
5 Replies

9. UNIX for Dummies Questions & Answers

Preventing wildcard expansion in "read"

Hi, I'm trying to use "read" to read in SQL lines and then apply to UDB. However KSH is expanding the *'s in the SQL into a list of all the files in the current directory... Anyway to turn this off? while read SQLStatement do echo "Running : " $SQLStatement... (8 Replies)
Discussion started by: gaijin 06
8 Replies

10. Shell Programming and Scripting

awk / bash globbing question

I would like to process a list of files matching: GPS*\.xyz with an awk script. I would then like to output the files to GPS*\.xyz.out (e.g. the same file name appended with .out). Something like: awk '{if(NR==1) {offset=-$1}; $1=$1+offset; print }' GPS*.xyz this does exactly what I want EXCEPT... (3 Replies)
Discussion started by: franzke
3 Replies

Featured Tech Videos