Thursday, October 11, 2012

Variable/Parameter Substitution in Shell Script

ISSUE:
You would like manipulate input argument in your shell script for many reasons.
for example
change input string to all upper case or lower case
create a log file according to prefix of script name

SOLUTION:
You can use various ways to do it (shell function, sed, awk).

PRACTICAL CASE:
1. convert string to lower case
${VARIABLE_NAME,,}

2. convert string to upper case
${VARIABLE_NAME^^}

3. get right of the .sh from script name
${0%.sh}

4.get string length
${#VARIABLE_NAME}

5. string position
${VARIABLE_NAME:postion} like ${1:0}

6. replacement
${VARIABLE_NAME/aa/bb/}

reference:
http://mintaka.sdsu.edu/GF/bibliog/latex/debian/bash.html
http://tldp.org/LDP/abs/html/parameter-substitution.html

No comments:

Post a Comment