I'm fairly new to linux scripting and apologize if this has been asked (did search quite a bit before posting).

I'm creating a simple script to check if certain letter combinations are occurring inside a backup file. The path to these files are device dependent and there are MANY of them so I have a list of the ones I need to check inside a text file.

What I would like to do, is cat the file and insert the name of the device inside the path to that devices folder and then check the latest backup file. e.g.

cat devicelist.txt  | grep A[BCD] /folder1/folder2/$1/latest

This gives me the error: /latest: No such file or directory

So what I'm doing wrong?


If I understand your requirement correctly, then you could use xargs e.g.

cat devicelist.txt | xargs -d'\n' -I{} echo grep 'A[BCD]' "/folder1/folder2/{}/latest"

or (reading the file directly)

xargs -a devicelist.txt -d'\n' -I{} echo grep 'A[BCD]' "/folder1/folder2/{}/latest"

Remove the echo once you are satisfied that it is doing what you want.

See man xargs for further information and a complete list of available options for your platform.


$1 means parameter (first) passed to the script

so you can use

grep A[BCD] /folder1/folder2/"$1"/latest

do not cat | grep when useless :) "$1" permit to preserve the variable content

-bash-4.4$ mkdir test
-bash-4.4$ mkdir -p test/folder/a/latest
-bash-4.4$ cat > t.sh
grep A[BCD] test/folder/"$1"/latest/toto
-bash-4.4$ chmod u+x t.sh
-bash-4.4$ echo ABCD > test/folder/a/latest/toto
-bash-4.4$ ./t.sh b
grep: test/folder/b/latest/toto: No such file or directory
-bash-4.4$ ./t.sh a
-bash-4.4$ rm t.sh
-bash-4.4$ rm -rf test

you may also preserve script to $1 values that do not exists if you want to avoid the error message from grep when folder path is not found.


You can use

grep 'A[BCD]' "/folder1/folder2/$1/latest" <devicelist.txt

in a script which you have called with an argument.

If you want to simulate the same behaviour in the shell then you have to do this before:

set -- foo

Then $1 becomes foo.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.