27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

OUTFILE=symlinks.list<br />

# save file<br />

directory=${1−`pwd`}<br />

# Defaults to current working directory,<br />

#+ if not otherwise specified.<br />

echo "symbolic links in directory \"$directory\"" > "$OUTFILE"<br />

echo "−−−−−−−−−−−−−−−−−−−−−−−−−−−" >> "$OUTFILE"<br />

for file in "$( find $directory −type l )" # −type l = symbolic links<br />

do<br />

echo "$file"<br />

done | sort >> "$OUTFILE"<br />

# stdout of loop<br />

# ^^^^^^^^^^^^^ redirected to save file.<br />

exit 0<br />

There is an alternative syntax to a for loop that will look very familiar to C programmers. This<br />

requires double parentheses.<br />

Example 10−12. A C−like for loop<br />

#!/bin/bash<br />

# Two ways to count up to 10.<br />

echo<br />

# Standard syntax.<br />

for a in 1 2 3 4 5 6 7 8 9 10<br />

do<br />

echo −n "$a "<br />

done<br />

echo; echo<br />

# +==========================================+<br />

# Now, let's do the same, using C−like syntax.<br />

LIMIT=10<br />

for ((a=1; a

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!