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 />

#:docstring strtok:<br />

# Usage: strtok s1 s2<br />

#<br />

# Strtok considers the string s1 to consist of a sequence of zero or more<br />

# text tokens separated by spans of one or more characters from the<br />

# separator string s2. The first call (with a non−empty string s1<br />

# specified) echoes a string consisting of the first token on stdout. The<br />

# function keeps track of its position in the string s1 between separate<br />

# calls, so that subsequent calls made with the first argument an empty<br />

# string will work through the string immediately following that token. In<br />

# this way subsequent calls will work through the string s1 until no tokens<br />

# remain. The separator string s2 may be different from call to call.<br />

# When no token remains in s1, an empty value is echoed on stdout.<br />

#:end docstring:<br />

###;;;autoload<br />

function strtok ()<br />

{<br />

:<br />

}<br />

#:docstring strtrunc:<br />

# Usage: strtrunc $n $s1 {$s2} {$...}<br />

#<br />

# Used by many functions like strncmp to truncate arguments for comparison.<br />

# Echoes the first n characters of each string s1 s2 ... on stdout.<br />

#:end docstring:<br />

###;;;autoload<br />

function strtrunc ()<br />

{<br />

n=$1 ; shift<br />

for z; do<br />

echo "${z:0:$n}"<br />

done<br />

}<br />

# provide string<br />

# string.bash ends here<br />

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

# ==> Everything below here added by the document author.<br />

# ==> Suggested use of this script is to delete everything below here,<br />

# ==> and "source" this file into your own scripts.<br />

# strcat<br />

string0=one<br />

string1=two<br />

echo<br />

echo "Testing \"strcat\" function:"<br />

echo "Original \"string0\" = $string0"<br />

echo "\"string1\" = $string1"<br />

strcat string0 string1<br />

echo "New \"string0\" = $string0"<br />

echo<br />

# strlen<br />

echo<br />

Appendix A. Contributed Scripts 395

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

Saved successfully!

Ooh no, something went wrong!