How to make * = '*' in bash?

Go To StackoverFlow.com

1

I'm a newbie in bash scripts and have the following problem : I'm trying to make a search script that let me search for files containing a certain string with find and / or grep and optional parameters, and I'm stuck with one last problem, the * character that doesn't match the condition in all cases.

There are 3 parameters to my script :

  • string_to_search (required)
  • file_name_pattern (optional)
  • path (optional)

Example :

./search_text.sh 'string_to_search' 'file_name_pattern' /path

I want to get the following :

  • If file_name_pattern is not specified or is * (with or without quotes) => grep
  • If file_name_pattern is anything else than * or '*' => find + grep

Here's my code :

#!/bin/bash

# ------------------------------------
# Set default values for optional parameters (tests)

if [ ! -z $2 ] 
then 
    FILE_TYPE=$2
    echo 'FILE_TYPE : var 2'
else
    FILE_TYPE="*"
    echo 'FILE_TYPE : *'
fi

if [ ! -z $3 ] 
then 
    LOCATION=$3
    echo 'LOCATION : var 3'
else
    LOCATION=.
    echo 'LOCATION : .'
fi

# ------------------------------------
# Do search / show conditions

if [ "$FILE_TYPE" == \* ];
  then 
    #grep -rn -C 0  --color=always  "$1" $LOCATION | less -R;
    echo ' => GREP';
  else
    #find $LOCATION -iname $FILE_TYPE | sort | xargs grep "$1" --color=always -n  2>/dev/null | less -R;
    echo '=> FIND';
fi

Just run this code to show the 2 echos at the end for testing.

Now my problem is :

./search_text.sh 'string_to_search' '*.php' /path  => works (FIND)
./search_text.sh 'string_to_search' '*' /path      => works (GREP)
./search_text.sh 'string_to_search' '*'            => works (GREP)
./search_text.sh 'string_to_search' *              => ERROR => goes to FIND INSTEAD OF GREP :-(
./search_text.sh 'string_to_search'                => works (GREP)

In other words how could I write the following condition :

   if [ "$FILE_TYPE" == \* ];

so that * should be considered the same as '*' when passing these values as a parameter in command line ?

Even escaping the * with \* the condition isn't matched when we only pass * without quotes to the script.

In other words, I'd like to make the * to be considered as a normal wildcard like in any other command, so that * = '*'

Many thanks for any help !

2012-04-05 17:16
by user1186503
Thanks to both Attila and Sorpigal ;-) Well, I thought there was a simple way to escape the * char and treat it as a string, but I will use it with quotes, thanks again for your hel - user1186503 2012-04-05 21:43


3

You can disable all glob expansion using

 set -o noglob

Afterwards * is not special:

$ ls *
ls: *: No such file or directory

However, there is no way to force your users to do this before calling your script. Since the user's shell, not your script, controls globbing you are left with no recourse.

Your only option would be to treat

./search_text.sh 'string_to_search' *

Exactly like

./search_text.sh 'string_to_search' file1 file2

Since that's what it is anyway.

In other words, when there is no trailing directory specified and $1 is not a literal * use the grep case.

2012-04-05 17:32
by Sorpigal


4

When you write * in bash, it is interpreted as file-matching in the current directory. Thus your script is actually called with ./search_text.sh 'string_to_search' a.txt b.txt (assuming there are only two files in the current dirtectory, named 'a.txt' and 'b.txt'). Because 'a.txt' is not '*', your find code is executed. The only way that I know of to prevent it is to enclose in apostophes (') -- as you did --, or escape it with backslash('\')

2012-04-05 17:28
by Attila
Ads