Bash basics: Difference between revisions

From Bitpost wiki
Line 41: Line 41:
I use an alias for common params, so just do this:
I use an alias for common params, so just do this:
  grep "my[ ]regex" source/code/directory/ --include="*.cpp"
  grep "my[ ]regex" source/code/directory/ --include="*.cpp"
Here's the full format, from [https://readwrite.com/2010/11/10/how-to-search-your-source-with/ this blog post]:
You can also use (and tweak) this helper:
  grep -inIEr –color=ALWAYS "my[ ]regex" source/code/directory/ --include="*.sh"
  cd src
grepcode blah


=== [http://www.panix.com/~elflord/unix/grep.html powerful grep options] ===
=== [http://www.panix.com/~elflord/unix/grep.html powerful grep options] ===

Revision as of 18:32, 8 July 2017

Bash certainly has its set of snafus.

store commands in variables

# NOTE don't put spaces around equals sign!
# NOTE don't use ampersands within command strings, they won't work
# use a blank command if needed, that's fine
mycmd1="command param param"
mycmd2="cmd2 param"
if [ -e /myspecialplace ]
mycmd2=""
fi
mycmd3="cmd3 param"
$mycmd1 && $mycmd2 && $mycmd3

capture output

myout=`program1 param`;
  • Note the difference a single quote makes to output...
$ eval echo "abc   def"
abc def
$ eval 'echo "abc   def"'
abc   def

check for an error

mkdir "$d2"
if [ $? -gt 0 ]; then
   echo "mkdir error, try that again..."
   exit 3
fi

read input from user

echo "Create the subdirectory?  [$d2]"
if read answer; then
    if [ ${answer} = "y" ] || [ ${answer} = "yes" ]; then
        {whatever}
    else
        {whatever}
    fi
fi

grep your code

I use an alias for common params, so just do this:

grep "my[ ]regex" source/code/directory/ --include="*.cpp"

You can also use (and tweak) this helper:

cd src
grepcode blah

powerful grep options