I'm trying to write a small script to get me list of files that match certain criteria into a file.

cd /var/www_data/patch/
grep --include=\*.{php,ini,conf,sh} -ril -P "'([\d\w\-\_\.]+)(@domain.com)'" "/var/www_data/something/" 2>&1 > mails

The grep line works perfectly in command line but if I launch it via script, resulting file will have size of 0 bytes. Any ideas why this is happening?

  • Do you have permission to write in this directory? – Sebastian Sep 7 '14 at 11:44
  • @Sebastian yes, of course. Thing is, even if comment out 2>&1 > mails part, it won't output anything into terminal – Igor Yavych Sep 7 '14 at 12:02
  • 6
    Your script contains bashisms (such as the brace expansion {php,ini,conf,sh}) but your shebang is #!/bin/sh. Are you sure /bin/sh is a bash shell on your system - it is often a symlink to a simpler shell such as dash. – steeldriver Sep 7 '14 at 13:22
  • @steeldriver oh yeah, indeed , it was supposed to be #!/bin/bash – Igor Yavych Sep 7 '14 at 13:33

Found my own mistake. It was supposed to be #!/bin/bash. Thanks to @steeldriver for the tip

  • 1
    Interesting definition of "found my own mistake" :) – Michael Mrozek Sep 7 '14 at 21:37

In order to enable shell expansion within a script you have to pass a command line option to bash, this can be done in the script itself:

#! /bin/bash
set -B   #enables expansion
set -X   #enables debugging

With the first option everything will be passed to the shell as if it were written in the command line, the second option will generate a lot of output, quite useful to see what happens but you surely want to disable it once your script is working properly.

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.