securitybreach Posted October 19, 2014 Share Posted October 19, 2014 The $ character is used for parameter expansion, and command substitution. You can use it for manipulating and/or expanding variables on demands without using external commands such as sed or awk. #1: Getting Up Default Shell Variables Value The syntax is as follows: ${parameter:-defaultValue} var=${parameter:-defaultValue} If parameter not set, use defaultValue. In this example, your shell script takes arguments supplied on the command line. You'd like to provide default value so that the most common value can be used without needing to type them every time. If variable $1 is not set or passed, use root as default value for u: u=${1:-root} Consider the following example: #!/bin/bash _jail_dir="${1:-/home/phpcgi}" echo "Setting php-cgi at ${_jail_dir}..." # rest of the script ... You can now run this script as follows: ./script.sh /jail # <--- set php jail at /jail dir ./script.sh /home/httpd/jail # <---- set php jail at /home/httpd/jail dir ./script.sh # <--- set php jail dir at /home/phpcgi (default) Here is another handy example: _mkdir(){ local d="$1" # get dir name local p=${2:-0755} # get permission, set default to 0755 [ $# -eq 0 ] && { echo "$0: dirname"; return; } [ ! -d "$d" ] && mkdir -m $p -p "$d" } Use this substitution for creating failsafe functions and providing missing command line arguments in scripts....... http://www.cyberciti...titution-2.html Quote Link to comment Share on other sites More sharing options...
abarbarian Posted October 19, 2014 Share Posted October 19, 2014 Neato article. I had a nosey at some of the other articles and there is some great stuff there. Nice clear human (as opposed to geek) understandable information on some great tips and tricks for penguins. I particularly like this, http://www.cyberciti.biz/faq/howto-compile-and-run-c-cplusplus-code-in-linux/ 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.