Note that there are some explanatory texts on larger screens.

plurals
  1. PORemoving final bash script argument
    primarykey
    data
    text
    <p>I'm trying to write a script that searches a directory for files and greps for a pattern. Something similar to the below <strong>except</strong> the find expression is much more complicated (excludes particular directories and files).</p> <pre><code>#!/bin/bash if [ -d "${!#}" ] then path=${!#} else path="." fi find $path -print0 | xargs -0 grep "$@" </code></pre> <p>Obviously, the above doesn't work because <code>"$@"</code> still contains the path. I've tried variants of building up an argument list by iterating over all the arguments to exclude path such as</p> <pre><code>args=${@%$path} find $path -print0 | xargs -0 grep "$path" </code></pre> <p>or</p> <pre><code>whitespace="[[:space:]]" args="" for i in "${@%$path}" do # handle the NULL case if [ ! "$i" ] then continue # quote any arguments containing white-space elif [[ $i =~ $whitespace ]] then args="$args \"$i\"" else args="$args $i" fi done find $path -print0 | xargs -0 grep --color "$args" </code></pre> <p>but these fail with quoted input. For example,</p> <pre><code># ./find.sh -i "some quoted string" grep: quoted: No such file or directory grep: string: No such file or directory </code></pre> <p>Note that if <code>$@</code> doesn't contain the path, the first script does do what I want.</p> <hr> <p><strong>EDIT</strong> : Thanks for the great solutions! I went with a combination of the answers:</p> <pre><code>#!/bin/bash path="." end=$# if [ -d "${!#}" ] then path="${!#}" end=$((end - 1)) fi find "$path" -print0 | xargs -0 grep "${@:1:$end}" </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload