How to grep for a string on a FILENAME?

I call my bash shell script "test.sh" and pass "admin_usr.txt" as an argument like below.

./test.sh admin_usr.txt

Inside the "test.sh" i wish to check if the filename passed "admin_usr.txt" i.e "$1" contains the string "admin" or not ... which in this case it does.

Note: I do not wish to search contents of the file but only the filename which is being passed as argument.-.

Can you please help me with the if statement ?

With more than 700 posts in 6 years, you should at least have a faint idea on how to approach this "problemcito", which you certainly don't hesitate to share so we can discuss...?

1 Like
if [ `echo $1 | grep -c "admin" ` -gt 0 ]

But i was looking for something simpler.

Still simpler? Not too many options... try

[ $1 = ${1/admin} ]
2 Likes
if echo "$1" | grep -q admin
1 Like

What i don't understand: if the filename has to contain "admin" and everything else is wrong, why don't you simply add this obligatory string to the parameter passed instead of checking if it is there?

like this:

filename="admin.${1}"

So if you call the script with

yourscript.sh foo

The filename would be "admin.foo", etc.

I hope this helps.

bakunin