I have a simple bash script, which receives a file with subject text, and processes line by line.
If the line begins with certain characters - then run block of compound commands. I am trying to use grep to test the lines for patterns (but would accept other suggestions).
#!/usr/bin/env bash
for i in "$@"
do
if grep "^M" $i # I want grep to "assume" $i was a file
# and test if the pattern "^M" is present
then
echo "This line started with an M: "$i
# command 1
# command 2
# etc
fi
done
D bar
M shell_test.sh
M another_file
Then run script with
cat subject_text.txt | xargs --delimiter="\n" ./matcher.sh
How can I get grep to treat each iteration $i
through the parameter list as if $i
were a file?
You can read the file Subject_text.txt
in a loop and feed matcher.sh
with the name of the file to check:
while IFS= read -r _ file_name
do
./matcher.sh "$file_name"
done < "Subject_text.txt"
However, now that I see, you are using matcher.sh
over every line. Note that calling a script with every single line as parameter is a bit overkilling.
What about looping over the file normally and performing the grep
?
#!/usr/bin/env bash
file=$1
while IFS= read -r line
do
if grep "^M" <<< "$line" # I want grep to "assume" $i was a file
# and test if the pattern "^M" is present
then
echo "This line started with an M: $i"
# command 1
# command 2
# etc
fi
done < "$file"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.