I have this shell script to update IP addresses in my configuration files (any that match
$old_address_pattern must be changed to
grep -rl "$old_address_pattern" /etc \ | xargs sed -i "s/$old_address_pattern/$new_address/g"
grep command finds no matching files, then
sed will complain 'no input files'. How can I make this pipeline succeed when the list of files is empty?
If you want to avoid running
grep produces no output, then (since you've tagged this with Ubuntu), you can give the <strong>
-r</strong> or <strong>
--no-run-if-empty</strong> argument to
<strong>--no-run-if-empty</strong><br /><strong>-r</strong><br /> If the standard input does not contain any nonblanks, do not run the command. Normally, the command is run once even if there is no input. This option is a GNU extension.</blockquote>
So your command should look like:<pre class="lang-sh prettyprint-override">
grep -rl "$old" /etc | xargs -r sed -i "s/$old/$new/g"