Translate

Archives

Case Insensitive File Listings

In general, Linux filenames are case-sensitive. So how case you list the files in your current directory in a case-insensitive way? Technically, you are looking to perform case-insensitive globbing. Here are some ways: # touch DEMO demo DeMo # ls -l [Dd][Ee][Mm][Oo] -rw-r–r–. 1 root root 0 Mar 24 11:39 demo -rw-r–r–. 1 root root 0 Mar 24 11:39 DeMo -rw-r–r–. 1 root root 0 Mar 24 11:39 DEMO # ls -l | grep -i demo -rw-r–r–. 1 root root 0 Mar 24 11:39 demo -rw-r–r–. 1 root root 0 Mar 24 11:39 DeMo -rw-r–r–. 1 root root 0 Mar

Shell String Concatenation

Many people are unaware that string concatenation using the += syntax is fully supported in modern versions of both the Bash and Korn shells Here is a simple example which demonstrates the syntax: str=”Hello” $ str+=” ” $ str+=”World” $ echo $str Hello World $ If you use this syntax with “numbers”, you end up with a string that looks like a number but is not a number. $ a=2 $ a+=4 $ echo $a $ 24

Korn Shell DEBUG Trap

The trap shell builtin is used to change the way signals are handled by the Korn Shell 93 (ksh93) shell. In addition, a trap may be set for three ksh93 pseudo-signals: EXIT, ERR, and DEBUG. In this post we demonstrate how to use the DEBUG pseudo-signal to trap changes in the value of a variable for debugging purposes. trap [ -p ] [ action ] [ sig ] . . . The -p option causes the trap action associated with each trap as specified by the arguments to be printed with appropriate quoting. Otherwise, action will be processed as if

Differences in Variable Scope in Shell Functions

Ksh93 and bash have subtly different scopes for variables defined in shell functions as the following example shows: # POSIX function syntax testme2() { printf “Function testme2 invokedn” var21=testme21 typeset var22=testme22 } function testme1 { printf “Function testme1 invokedn” var11=testme11 typeset var12=testme12 } testme1 echo “VAR11=$var11” echo “VAR12=$var12” Here is the output when run under ksh93: Function testme1 invoked VAR11=testme11 VAR12= Function testme2 invoked VAR21=testme21 VAR22=testme22 and here is the output when run under bash: Function testme1 invoked VAR11=testme11 VAR12= Function testme2 invoked VAR21=testme21 VAR22= Note the different output for var22! Ksh93 has lexical scoping. A variable is normally global

Source a Function in KSH93

Ksh93 allows you to source a file or a function into local scope. Other shells support sourcing a file but ksh93 is the only shell that I am aware of that allows you to also invoke a function with the source command. From the KSH93 manpage: . name [ arg … ] If name is a function defined with the function name reserved word syntax, the function is executed in the current environment (as if it had been defined with the name() syntax.) Otherwise if name refers to a file, the file is read in its entirety and the commands