Bash basics: Difference between revisions
(Created page with "Bash certainly has its set of snafus. * To store commands in variables: <pre># NOTE don't put spaces around equals sign! # NOTE don't use ampersands within command strings, they...") |
No edit summary |
||
(13 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
Bash | DO NOT USE BASH IF YOU HAVE ANOTHER OPTION. Bash is a bucket of snafus. | ||
=== proper way to exit mid-script === | |||
kill -SIGINT $$; | |||
=== squelch ALL output === | |||
noisycommand &>/dev/null | |||
=== store commands in variables === | |||
<pre># NOTE don't put spaces around equals sign! | <pre># NOTE don't put spaces around equals sign! | ||
# NOTE don't use ampersands within command strings, they won't work | # 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" | mycmd1="command param param" | ||
mycmd2="cmd2 param" | mycmd2="cmd2 param" | ||
Line 12: | Line 20: | ||
$mycmd1 && $mycmd2 && $mycmd3 | $mycmd1 && $mycmd2 && $mycmd3 | ||
</pre> | </pre> | ||
=== capture output === | |||
<pre>myout=`program1 param`;</pre> | |||
* Note the difference a single quote makes to output... | |||
<pre>$ eval echo "abc def" | |||
abc def | |||
$ eval 'echo "abc def"' | |||
abc def | |||
</pre> | |||
=== check for an error === | |||
<pre>mkdir "$d2" | |||
if [ $? -gt 0 ]; then | |||
echo "mkdir error, try that again..." | |||
exit 3 | |||
fi | |||
</pre> | |||
=== read input from user === | |||
<pre>echo "Create the subdirectory? [$d2]" | |||
if read answer; then | |||
if [ ${answer} = "y" ] || [ ${answer} = "yes" ]; then | |||
{whatever} | |||
else | |||
{whatever} | |||
fi | |||
fi</pre> | |||
=== 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 | |||
Here's the alias: | |||
alias grep='grep -inIEr --color=ALWAYS' | |||
=== [http://www.panix.com/~elflord/unix/grep.html powerful grep options] === | |||
=== misc === | |||
{| class="mw-collapsible mw-collapsed wikitable" | |||
! bash chmod dirs | |||
|- | |||
| | |||
find /path/to/base/dir -type d -exec chmod g+x {} \; | |||
|} |
Latest revision as of 15:42, 15 March 2019
DO NOT USE BASH IF YOU HAVE ANOTHER OPTION. Bash is a bucket of snafus.
proper way to exit mid-script
kill -SIGINT $$;
squelch ALL output
noisycommand &>/dev/null
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
Here's the alias:
alias grep='grep -inIEr --color=ALWAYS'
powerful grep options
misc
bash chmod dirs |
---|
find /path/to/base/dir -type d -exec chmod g+x {} \; |