In this article I'll show you a more complex script than the script I have shown in the article dos2unix bash script. This new script allow you to convert more than one file at the same time.
Here is the script:
#!/bin/bash
###############################################################################
# dos to unix bash script #
# 2009 www.xAppSoftware.com #
# Luigi D'Andrea #
#-----------------------------------------------------------------------------#
# save this script, then chmod +x the script, the you are ready to run it #
#-----------------------------------------------------------------------------#
# Licence: #
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY #
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED #
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE #
# DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY #
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES #
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND #
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT #
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF #
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #
###############################################################################
case "$#" in
0) echo "Usage: `basename $0` <files_to_convert>";
exit;;
esac
for f in "$@"
do
echo $f
cat $f | tr "\015" "\012" > $f.new
mv "$f.new" "$f"
done
I would add
<pre>if [ ! -f "$f" ]; then
echo "Error: $f not found"
else
echo "$f"
cat "$f" | tr "15" "12" > "$f.new"
mv "$f.new" "$f"
fi</pre>