ksh - Korn Shell example

Category: /knowledge /linux
Tags: linux

set local variables

no export, no set

CLASSPATH=.:/app:\
/opt/lib/log/log4j-1.2.15.jar
echo $CLASSPATH

get parameter from command line

print “Enter your user ID:\c” read yourID

case control

case $var in 
  *some_value)
    exec $some_cmd
    ;;
  another_value)
    exec $other_cmd
    ;;
esac

if control

Note: the space after and before [] is very important.

if [ -f $fn ]; then
  echo "the file exists"
else
  echo "the file doesn't exist"
fi

check input parameters

if [ $# -gt 0]; then
  echo "parameter size=$#"
  RT_ENV=$1
else
  echo "no parameter"
fi

File tests

-a file - file exists
-d file - file is a directory
-f file - file is a regular file
-r file - You have read permission on file
-s file - file exists and is not empty
-w file - You have write permission on file
-x file - You have execute permission on file
-O file - You own file
-G file - Your group ID is the same as that of file
file1 -nt file2 - file1 is newer than file2 (based on modification date)
file1 -ot file2 - file1 is older than file2

get absolute path

BASEDIR=$(cd $(dirname $0); pwd)
echo "Current dir: $BASEDIR"

get 1st part of the host name

HOSTNAME=`uname -n | cut -d . -f 1`
echo "HOSTNAME=$HOSTNAME"

Array example

Note: {} here is a must

urls[0]="url_1"
urls[1]="url_2"
urls[2]="url_3"

for url in ${urls[*]}; do
    echo $url
done

讨论

提示

  • 如果看不到讨论部分, 请暂时关掉adblock in Firefox/Chrome
  • 本网站使用Javascript实现评论功能, 此处外链对提高您的网站PR没有帮助. (潜台词: 请不要灌水, 谢谢)