How to determine the use of a wildcard (asterisk *) as a parameter for a shell script?

In my script, how can I tell when an asterisk was used instead of strongly typed parameters?

it

# myscript *

from this

# myscript p1 p2 p3 ... (where parameters are unknown number)
+3
source share
6 answers

The shell extends the template. By the time the script was run, the wildcard was expanded, and there is no way for the script to determine if the arguments were wildcards or an explicit list.

This means your script will need help from something else that is not a script. In particular, something that runs before command line processing. This is a bit of an alias. This is your nickname.

alias myscript='set -f; globstopper /usr/bin/myscript'

, "myscript", , - "myscript", , . : -, set -f, globstopper, script .

, globstopper? :

globstopper() {
  if [[ "$2" == "*" ]]
    then echo "You cannot use a wildcard"
    return
  fi
  set +f
  "$@";
}

. -, , script (caveat: , , , , , ). -, . , .

, script. , .

, : ".

+7

, , , script , .

Tom , , . , script findin,

#!/bin/bash
echo "[${1}]"

, :

$ findin *

,

[*]

,

alias findin='set -f; /path/to/findin'

. , , (, ls -lh *.py). ,

echo $-

. f, .

,

set +f

findin, .

, script (set + f), , , :

g(){ /usr/local/bin/findin "$@"; set +f; }
alias findin='set -f; g'

: "g" , .

, , - :

reset_expansion(){ CMD="$1"; shift; $CMD "$@"; set +f; }
alias findin='set -f; reset_expansion /usr/local/bin/findin'

, script, , ,

alias newscript='set -f; reset_expansion /usr/local/bin/newscript'

.

, , . .


+7

.

(, , ) Unix.

. " UNIX-HATERS" .

+3
$arg_num == ***; // detects *(literally anything since it a global wildcard)
$arg_num == *_*; // detects _

_

for i in $*
  do
    if [[ "$i" == *_* ]]; 
      then echo $i; 
    fi
  done

./bash test * test2 _

_

./bash test * test2 *********, ****

test
bash
pass.rtf
test2
_

: * bash, , , . , , - - , * .

+1

, :

alias c='set -f; call_clc'

where "call_clc" is the function: "function call_clc { clc "$*"; set +f; }"

and "clc" is the script
#!/bin/bash
echo "$*" | sed -e 's/ //g' >&1 | tee /dev/tty | bc

"set -f" "set +f", , "c 4 * 3", , , bash.

Update: the previous option ' alias c='set -f; clc "$*"; set+f;'' did not work because for some reason the correct result was obtained after calling the "c 4 * 4" command twice. Any idea why this is so?

+1
source

If this is what you feel you should do, perhaps:

# if the number of parms is not the same as the number of files in cwd
# then user did not use *
dir_contents=(*)
if [[ "${#@}" -ne "${#dir_contents[@]}" ]]; then 
    used_star=false
else
    # if one of the params is not a file in cwd
    # then user did not use *
    used_star=true
    for f; do [[ ! -a "$f" ]] && { used_star=false; break; }; done
fi
unset dir_contents

$used_star && echo "used star" || echo "did not use star"

Pedantically, this will reflect the "star used" if the user actually used the asterisk or if the user manually entered the contents of the directory in any order.

0
source

Source: https://habr.com/ru/post/1757695/


All Articles