1

I'm trying to learn Bash scripting. when i use the command directly in the bash

find . -xdev -type d -path ./sys -o -path ./proc -o -path ./run -prune -o -iname "*.txt"

This gives me a buch of results while

But when I use the same code in a script file

#!/bin/bash

shopt -s extglob

find . -xdev -type d -path ./sys -o -path ./proc -o -path ./run -prune -o -iname "$1"
~         

Gives me only one result.

PS: Don't take the extglob seriosly I was trying to do it with parentheses.

Tried to add shopt -s extglob, tried different variants of code. Command with parentheses didn't work even when I added shopt -s extglob

Created a file named "bul" and put the code inside, chmoded it with +x and put the file in ~/.local/bin where i want to run directly from my terminal(I have the path in my .bashrc)

I removed shopt -s extglob from the code,

I have text.txt file in my home folder and 2 .txt files in my ~/Documents/ folder. It's only finds the file in the home folder. where i search from. Even when i it must start searching from / root.

EDIT: When i search directly from terminal with command:

find . -xdev -type d '(' -path './sys/*' -o -path './proc/*' -o -path './run/*' ')' -prune -o -name *.txt -print

It's gives me the correct result.

But when i run it wild wildcard with command bul . *.txt it's give me the result:

+ find . -xdev -type d '(' -path './sys/*' -o -path './proc/*' -o -path './run/*' ')' -prune -o -name text.txt -print

It's searching "text.txt"

SOLVED: When i type the command in terminal like "bul . .txt" it doesn't work but when i qouted second argument 'bul . ".txt"' like this it works.

Thank you all for your help.

srctghn
  • 11
  • 2
  • `use the same code` but it has `"$1"` – KamilCuk Jun 13 '23 at 19:08
  • 1
    please update the question with the *`one result`* generated by the script as well as a sample of what's generated when run at the command prompt; also, confirm whether or not you issued `shopt -s extglob` prior to running the command at the command prompt; also, please show how you're calling the script (of particular interest is the parameter(s) supplied to the script – markp-fuso Jun 13 '23 at 19:09
  • 5
    How are you running the script? If you want to pass a filename wildcard as `$1`, you have to quote it. `./scriptname '*.txt'` – Barmar Jun 13 '23 at 19:09
  • 5
    Otherwise the shell expands the wildcard, and `$1` just contains the first matching name. – Barmar Jun 13 '23 at 19:09
  • 2
    When debugging a script, one of the first steps is to put `set -x` at the beginning. Then you'll see the expansion of the variables, which should clue you into the problem. – Barmar Jun 13 '23 at 19:11
  • 1
    Put a `echo $1` at the beginning of your script to see what is actually in `$1` – Ljm Dullaart Jun 13 '23 at 21:48
  • Why are you expecting the **bash** extglob option to control how **find** does wildcard matching? – Shawn Jun 13 '23 at 21:56
  • 1
    Confirm you using the same directory as the `.` value in your find command. You could replace the `.` with a hard-coded path in both usages, i.e. `find /path/to/start/search ....` . Good luck. – shellter Jun 13 '23 at 23:04
  • You were given a correct explanation of the problem in Barmar's comment 22 hours ago now. Why is this question still being edited? – Charles Duffy Jun 14 '23 at 17:59
  • @LjmDullaart, no, `echo $1` is actively misleading. It would need to be `echo "$1"` -- see [I just assigned a variable, but `echo $variable` shows something else](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else). – Charles Duffy Jun 14 '23 at 18:00

0 Answers0