Chunks of bash shell programming

I am going to provide a chunks of codes that I do not understand. Please help with them in a layman's terms.

1) ${DEBUG:-0}

--------------------------------------------------------------------------

2) print "${1}"

--------------------------------------------------------------------------

3) function logmsg {
    if [ ! -f ${MAILFILE} ];
    then
        start_mail
    fi
    print "${1}" | ${TEE} -a ${MAILFILE}
    }

--------------------------------------------------------------------------

4) function debugmsg {
    if [ ${DEBUG} -ne 1 ];
    then
        return
    fi
    logmsg "${1}"
    }

--------------------------------------------------------------------------

5) H="How are you"

--------------------------------------------------------------------------

6) if [ ! -z "${msg}" ];

--------------------------------------------------------------------------

This looks almost like homework so I'm not gonna go into much details. If you have any specific questions post them back here.

  1. Expand the variable "DEBUG" if, and only if it's already defined, else use zero.
  2. Print the contents of the first script argument.
  3. Defines a function that checks if a file does not exist. "tee" is a specific purpose command; Take a look at the man page (man tee).
  4. Defines another function that checks if the contents of $DEBUG aren't equal to 1 (numeric value, not string).
  5. String assignment.
  6. "-z" is a comparison operator used to tell if string has zero length.

In all cases the syntax ${var} is used to "protect" the variables. E.g:

var1="asdf"
echo "${var1}2"
echo "$var12"

One of the above "echo" statements will work fine and the other will throw an error. Try it for yourself to find out which one fails and why. :wink:

Thanks. I do not have a way to try those codes. In the office, I do not want to try it out in production box. In my personal laptop, I do not even have unix. But with whatever little knowledge I have, I can say that

  1. var1="asdf"

  2. echo "${var1}2"

  3. echo "$var12"

  4. will work because we are just assigning some characters to a variable named var1.

  5. will work. It will give the result as asdf2.

  6. will not work as there is no such variable named var12.

I hope you did not give these codes to show that I don't know anything about shell scripting. Though I myself admit that I know nothing about shell scripting. But I would like to learn it quite fast !

No one around here will post a code just to show off or make other people feel bad for not being able to write a script or solve X or Y problem.

You're welcome (and encouraged) to post any questions you may have.

Homework is still welcome but some restrictions apply -- usually in those cases people are asked to show some effort on the issue and not just ask "please do my homework".

In any case, you guessed right about the example code. Sorry if I thought this was homework.

Edit: Also, kudos for not running the above in a production box. Although the code is 100% safe, it's good to know that nobody is using a production box as a lab environment. More than once I've seen someone "accidentally" change the hostname to � (that's a backtick) or similar because they were learning how to use the "hostname" command in a production server.

I think the best progress you can make in *nix scripting is playing around with it (did you try to install "busybox" on your personal laptop?) while reading man pages (fourth item on this forum's top menu) and solving/copying problem examples like in these forums.

I suggest busybox for windows for that problem. Run busybox bash in a CMD prompt and you'll have a fairly complete UNIX shell along with most of the utilities people expect to use with one.

It's not actually UNIX though, so there'll still be some fundamental differences (case-insensitive files, no special device files or named pipes, etc, etc)

A=${0##*/}
S=${0%/*}

Please explain me with examples for the above 2 statements.

The shell sets $0 to the name used to invoke the script that is running. So, if you script is named test.sh and it is located in the directory /home/user/bin , $0 could expand to test.sh , ./test.sh , /home/user/bin/test.sh , or several other possibilities.

${variable##pattern} will expand to the value of $variable with the longest string matching the pattern pattern removed from the beginning of the string. If pattern doesn't match anything at the start of the string, the result is the same as the expansion of ${variable} . So if $0 expands to test.sh , ./test.sh , or /home/user/bin/test.sh ; ${0##*/} will expand to test.sh .

Similarly, ${variable%pattern} expands to the expansion of $variable with the shortest match for pattern removed from the end. So, for the same values of $0 above, ${0%/*} will expand to test.sh , . , and /home/usr/bin , respectively.