https://splike.com/w/api.php?action=feedcontributions&user=BillAlbing&feedformat=atomsplike.com - User contributions [en]2024-03-28T12:48:53ZUser contributionsMediaWiki 1.24.1https://splike.com/w/index.php?title=Bash_Scripting_FAQ&diff=1466Bash Scripting FAQ2007-06-18T13:59:08Z<p>BillAlbing: add link to Bash Cookbook</p>
<hr />
<div>Other resources:<br />
<br />
* See also the [http://www.faqs.org/faqs/unix-faq/shell/bash/ official Bash FAQ].<br />
* Bash's [http://www.opensource.apple.com/darwinsource/10.3/bash-29/bash/examples/ examples directory] has lots of useful examples.<br />
* [http://www.faqs.org/docs/abs/HTML/index.html Advanced Bash-Scripting Guide]<br />
* [http://www.bashcookbook.com/ Bash Cookbook]<br />
* A [http://www-106.ibm.com/developerworks/library/l-bash.html good intro tutorial] by Dan Robbins, Gentoo founder.<br />
* Of course the [http://www.gnu.org/software/bash/manual/bash.html official manual] may come in handy too.<br />
<br />
<br />
__TOC__<br />
<br />
<br />
=== Loop over some command's output ===<br />
<br />
Loop breaking on any whitespace<br />
<pre><br />
for word in `cat somefile`<br />
do<br />
echo Do something with $word here.<br />
done<br />
</pre><br />
<br />
Loop breaking on lines (useful if directories have spaces in them)<br />
<br />
<pre><br />
find . -iname "*.jpg" | while read file<br />
do<br />
echo Do something with $file here.<br />
done<br />
</pre><br />
<br />
=== How do I check to see if an external executable is available? ===<br />
<br />
<br />
<br />
Replace <code>jhead basename find mkdir mogrify cp</code> with the list of executables you want to check for. This snippet was taken from Thomas Hopfner's img2html script.<br />
<pre><br />
for tool in jhead basename find mkdir mogrify cp; do<br />
if ! type $tool >/dev/null 2>&1; then<br />
echo "ERROR: \"$tool\" not found."<br />
echo " This is needed by $scriptname to work. Check your"<br />
echo " \$PATH variable or install the tool \"$tool\"."<br />
exit 2<br />
fi<br />
done<br />
</pre><br />
<br />
=== How do I get the short name of this script ===<br />
<br />
scriptname=`basename "$0"`<br />
<br />
Or better:<br />
<br />
scriptname=<br />
<br />
<br />
<br />
=== How can I debug? ===<br />
<br />
<br />
<br />
To see the source as it executes through it, run your script like this:<br />
<br />
bash -v script.sh<br />
<br />
or add this to the beginning of your script:<br />
<br />
set -v<br />
<br />
To see the expressions evaluated as the script runs, execute your script like this:<br />
<br />
bash -x script.sh<br />
<br />
or add this to the beginning of your script:<br />
<br />
set -x<br />
<br />
<br />
=== How do I get my quoting right? (file paths with spaces; security concerns) ===<br />
<br />
<br />
<br />
When executing anything, quote your variables if they are a single argument. Bash will quote the variable for you.<br />
<br />
mkdir -p "$destdir"<br />
<br />
Don't quote if the argument variable might be empty, or if the variable is more than one parameter to the executable. In the following example, if <code>$recursive</code> is set, we get <pre>find . '' -iname "*.jpg" ...</pre> and if <code>$recursive</code> is not set, we get <pre>find . '-maxdepth 1' -iname "*.jpg" ...</pre> Neither of these is correct. In this case, we should not quote $recursive_param.<br />
<pre><br />
if [ "$recursive" ]<br />
then<br />
recursive_param=""<br />
else<br />
recursive_param="-maxdepth 1"<br />
fi<br />
# this is bad; $recursive_param should not be quoted<br />
find . "$recursive_param" -iname "*.jpg" -or -iname "*.gif" -or -iname "*.png" | while read file<br />
do<br />
# ...<br />
done<br />
</pre><br />
<br />
When doing tests (if and case statements), quote your variables:<br />
<pre><br />
if [ ! "$force" ] && [ -e "$destfile" ]<br />
then<br />
if [ "$verbose" ]<br />
then<br />
echo Skipping $destfile<br />
fi<br />
fi<br />
</pre><br />
<br />
<br />
=== Concatenate strings? ===<br />
<br />
fullfilepath="$dirname$basename"<br />
<br />
Or: <br />
<br />
fullfilepath="doesnt_change/${dirname}also_doesnt_change/${basename}.log"<br />
<br />
( assuming $dirname includes a trailing '/' )<br />
<br />
=== Substring function? ===<br />
<br />
Use <code>${var:offset:length}</code><br />
<pre><br />
myvar=abcdef<br />
slice_of_myvar=${myvar:3:2}<br />
</pre><br />
<br />
=== Get a file's basename, dirname, extension, etc? ===<br />
<br />
See [http://www.opensource.apple.com/darwinsource/10.3/bash-29/bash/examples/functions/basename2 bash/examples/functions/basename] and [http://www.opensource.apple.com/darwinsource/10.3/bash-29/bash/examples/functions/basename2 bash/examples/functions/basename2]<br />
<pre><br />
# set the 'file' variable first<br />
<br />
# get extension; everything after last '.'<br />
ext=${file##*.}<br />
<br />
# basename<br />
basename=`basename "$file"`<br />
# everything after last '/'<br />
basename=${file##*/}<br />
<br />
# dirname<br />
dirname=`dirname "$file"`<br />
# everything before last '/'<br />
basename=${file%/*}<br />
</pre><br />
<br />
<br />
=== Include another script (e.g. a script of functions)? ===<br />
<br />
<br />
''Note: Test this; add full example''<br />
<br />
source util_functions.sh<br />
<br />
<br />
=== Check to see if script is running as root? ===<br />
<br />
<pre><br />
if [ `id -u` != "0" ]; then<br />
echo "Sorry, you are not root."<br />
exit 1<br />
fi<br />
</pre></div>BillAlbing