1

I am trying to write a script that will let me get the arguments while running the script. I am able to do so with $@ in the script. But if I run the script with /* or */* it gives the list of all the directories in /* or */*.

What I want to get as a variable in the script, is the value /* and */* if used; instead of all directories and files within /*, whenever the script is run.

I know that the issue is with the way linux fetches the values of what is associated with the "*". But did not find a solution for it.

Below is a sample script:

#!/bin/bash int_v=0 get_str="$@" for f in $get_str do if [ "$f" == "/*" ] || [ "$f" == "*/*" ] || [ "$f" == "/" ] || [ "$f" == "/boot" ] || [ "$f" == "/bin" ] || [ "$f" == "/root" ] || [ "$f" == "/dev" ] || [ "$f" == "/etc" ] || [ "$f" == "/sbin" ] || [ "$f" == "/home" ] || [ "$f" == "/mnt" ] || [ "$f" == "/sys" ] || [ "$f" == "/tmp" ] || [ "$f" == "/usr" ] || [ "$f" == "/var" ] || [ "$f" == "/proc" ]; then int_v=1 else int_v=0 fi echo $f done if [ $int_v == 1 ]; then echo "Cannot run it with the use of: */* or /* or /. " else echo "This script can run" fi 

Also added a work around in the script: Since /* is the root directory; I have added an if statement for /bin, /root ... This works, but the concern is with the */*.

Anyone has an idea of how we can parse the actual variables used while running the script, rather than the output of it?

1
  • Your script is subject to normal shell syntax and semantics, and in normal shell semantics somecommand /* means "run somecommand, and pass as arguments a list of files directories under /". Your command cannot make itself special and give that command line some other meaning. You need to pass and take arguments in ways that conform to standard shell syntax, not try to somehow override it. Commented Apr 23, 2020 at 17:14

1 Answer 1

2

When you run the script from a shell, the shell expands the wildcards before the script is run (see Pathname Expansion in man bash). So, the script never sees */*, it already sees all the expanded files.

To pass wildcard expression literally to the script, quote it or backslash it:

myscript.sh '*/*' myscript.sh \*/\* 

You need to properly doublequote all the variables that hold the wildcard expressions if you want to prevent the expansion of the wildcards, as variable expansion happens before pathname expansion (see Expansion in man bash).

a=* # No expansion, assignments are special. echo $a # Expands the wildcard. echo "$a" # Outputs *. 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.