0

I am trying to strip wildcards from user input text in a c-shell script.

I realize that the shell automatically expands all wildcards prior to storing the data.

Is there a way to isolate the non-wildcard portion of user input?

I also realize that c-shell is not the best shell for scripting. Unfortunately, the script is already written and I'm merely modifying.

6
  • 1
    Wildcards, along with redirection, is handled by the parent shell. Programs don't see wildcards, the see the expanded file list. By the time your program starts, there is no way to tell which parameters came from wildcards, and which didn't. Are you sure you understood the assignment? Did I undrstand what you want?
    – waltinator
    Commented Oct 12, 2023 at 18:21
  • Yes, you understood what I'm asking. I was afraid that was the answer. My assignment is to take all files input and create one report for all. In an attempt to name the report logically, I was hoping to use the root part of a wildcard input.
    – Eric
    Commented Oct 13, 2023 at 19:40
  • 1
    Find another way. You would have to inspect ALL of the filenames and deduce the maximal wildcard. Also, how would subsequent uses of the data find the file? Read man date, and incorporate a timestamp into the filrename, IMHO.
    – waltinator
    Commented Oct 13, 2023 at 19:56
  • Since all wildcards are also valid pathnames, there is no way for a script to distinguish between a pathname and a wildcard other than to possibly expand the wildcard and look at the matching pathnames. Even if you do this, the wildcard *.txt may match a file called *.txt (literally) and other files with the .txt filename suffix. It is in that case difficult to know whether the original string was a wildcard or a valid file name. The script needs to be specific with what type of data it expects from the user (e.g. "only filenames"), and then assume that the user follows those rules.
    – Kusalananda
    Commented Oct 25, 2023 at 14:10
  • I think you should disable globbing. For do it, set -f can help you. Commented Oct 25, 2023 at 14:40

1 Answer 1

0

If you mean to remove all the *, ?, [ and ] characters from some $input variable, you could do:

% set input = '**-[qwerty]-??'
% set stripped = $input:as/?//:as/*//:as/[//:as/]//:q
% printf '%s\n' $stripped:q
-qwerty-
2
  • Thanks for your reply. Since I need to operate on user input, I was only able to get this to work if I double quoted the input variable ("$1") and force the user to double quote the command line argument. > ls -1 test* test.csh test.zip test2.gz > cat test.csh #! /bin/csh set input = "$1" echo $input set stripped = $input:as/?//:as/*//:as/[//:as/]//:q printf '%s\n' $stripped:q > test.csh "test*" test.csh test.zip test2.gz test >
    – Eric
    Commented Oct 26, 2023 at 17:36
  • @Eric, in csh, you need $var:q, not "$var" to pass the contents of a variable verbatim to a command, including set. Compare csh -c 'echo "$1"' $'a\nb' vs csh -c 'echo $1:q' $'a\nb' from a Korn-like shell such as bash or zsh. So set input = $1:q, not set input = "$1" Commented Oct 26, 2023 at 17:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .