Linux commands 一覽
來源網站
• Linux命令:Accept
• Linux命令:Atq
• Linux命令:Bg
• Linux命令:Bunzip2
• Linux命令:Bzip2
• Linux命令:Chattr
• Linux命令:Chgrp
• Linux命令:Clear
• Linux命令:Colrm
• Linux命令:Comm
• Linux命令:Cut
• Linux命令:Dd
• Linux命令:Df
• Linux命令:Diff3
• Linux命令:Factor
• Linux命令:Fc
• Linux命令:Finger
• Linux命令:Fmt
• Linux命令:Fold
• Linux命令:Fuser
• Linux命令:Gencat
• Linux命令:Halt
• Linux命令:Head
• Linux命令:Hostname
• Linux命令:Jobs
• Linux命令:Join
• Linux命令:Kill
• Linux命令:Ldd
• Linux命令:Less
• Linux命令:Lex
• Linux命令:Ln
• Linux命令:Locate
• Linux命令:Look
• Linux命令:Lsmod
• Linux命令:Man
• Linux命令:Mcd
• Linux命令:Mkdir
• Linux命令:More
• Linux命令:Mv
• Linux命令:Newgrp
• Linux命令:Nfsstat
• Linux命令:Nl
• Linux命令:Passwd
• Linux命令:Paste
• Linux命令:Pathchk
• Linux命令:Pstree
• Linux命令:Ptx
• Linux命令:Reboot
• Linux命令:Reject
• Linux命令:Shutdown
• Linux命令:Sort
• Linux命令:Sudo
• Linux命令:Tail
• Linux命令:Tc
• Linux命令:Tee
• Linux命令:Timeconfig
• Linux命令:Top
• Linux命令:Tree
• Linux命令:Tty
• Linux命令:Umask
• Linux命令:Uname
• Linux命令:Uniq
• Linux命令:Unzip
• Linux命令:Uptime
• Linux命令:Wc
• Linux命令:Whereis
• Linux命令:Which
• Linux命令:Whois
• Linux命令:Yes
• Linux命令:Ypcat
• Traceroute
2008年7月31日 星期四
Shell script Examples
Shell script Examples
轉貼自此
SH 教學
1. 一個簡單的例子
#!/bin/sh
echo "** Non-root UID=0 or GID=0 accounts:"
grep ':00*:' /etc/passwd | \
awk -F: 'BEGIN {n=0}
$1 != "root" {print $0 ; n=1}
END {if (n==0) print "None found."}'
---------------------------------------------------------------------
2. shift 的用法,及一些特殊符號的意義
#!/bin/sh
echo $1 $2 $3
echo $#
# will be 4
shift
echo $1 $2 $3
echo $#
# will be 3
# "$*" = "$1 $2 $3....$n"
echo $*
# "$@" = "$1" "$2" "$3" "$4" ..."$n"
echo $@
#
# $? Exit status of previous command
# $$ PID of this shell's process
# $! PID of the most recently started backgroup job
echo $?
echo $$
echo $!
---------------------------------------------------------------------
3. 變數設定的方法,及特殊設定
#!/bin/sh
item=aaaa
item1=bbbb
echo ${item}1 $item1
#!/bin/sh
name=rache1
echo ${name-tatiana}
# show rache1
echo ${name2-tatiana}
# show tatiana
echo ${name=tatiana}
# show rache1
echo ${name2=tatiana}; echo $name2
# show tatiana
# tatiana
dir=${name5-`pwd`}; echo $dir
---------------------------------------------------------------------
4. if 的用法,有很多比較方法 -s, -r, -f, -d .. 請看 man if or man test
例如:底下兩敘述是相同的
if test -z "$var"; then ...
if [ -z "$var" ]; then ...
#!/bin/sh
# in sh , echo -n 只有當 /usr/ucb 路徑在 /usr/bin 之前才有用
if [ -f /usr/bin/ls ]; then
echo -n " ls command found "
fi
#!/bin/sh
strings /vmunix | grep UNIX > /tmp/motd
head -1 /etc/motd | grep UNIX > /tmp/th
if [ -z /tmp/th ]
then
cat /etc/motd >> /tmp/motd
else
tail +2 /etc/motd >> /tmp/motd
fi
mv /tmp/motd /etc/motd
---------------------------------------------------------------------
5. 再看一個加上變化的 if
#!/bin/sh
set `who -r`
if [ $9 = "S" ]; then
echo "The system is coming up. Be patient."
elif [ $7 = "2" ]; then
echo "Changing to state 2."
else
echo "Changing to state 3."
fi
底下是更多的 if 的例子
if [ $9 = "S" ]
if [ -s /etc/ptmp ]
if [$# -lt 4 ]
if [ ! -f /etc/.fscksk ]
if [ $? -eq 0 ]
if [ $? -ne 0 ]
if test -z "$var"
if [ -z "$var" ]
---------------------------------------------------------------------
6. 還是 if 的例子
#!/bin/sh
pid=`/bin/ps -e | grep 'lpsched$' | sed -e 's/^ *//' -e 's/ .*//'`
echo ${pid}
if [ "${pid}" != "" ]
then
echo $pid
/bin/kill ${pid}
fi
if [ -r /fastboot ]; then
echo "skip the fsck"
else
echo "do the fsck"
fi
if [ -d /etc/rc0.d ]
then
echo "run the K files"
fi
if [ -x /etc/inetd ]
then
echo "inetd"
fi
if [ "${BOOT}" = "yes" -a -d /etc/rc0.d ]
then
echo "run /etc/rc0.d"
fi
---------------------------------------------------------------------
7. while 的語法,與讀進參數
#!/bin/sh
cat /etc/vfstab |
while read DEVICE MOUNT_DIR READONLY FS DUMMY1 DUMMY2
do
echo "$DEVICE, $MOUNT_DIR, $READONLY, $FS, $DUMMY1, $DUMMY2"
done
---------------------------------------------------------------------
8. case 的用法,和 csh 真的差很多
$? 是執行命令後的 return status , 0: succeed , 1: false
#!/bin/sh
#/etc/fsck -p > /dev/console
ls / > /dev/null
case $? in
0)
echo "return 0"
;;
2)
exit 1
;;
4)
echo "return 4"
;;
*)
echo "Unknown error in reboot" > /dev/console
exit 1
;;
esac
---------------------------------------------------------------------
9. 再一個 case 的範例
# can get the same result when "$1" or $1
#case $1 in
case "$1" in
'start')
echo "start"
;;
'stop')
echo "stop"
;;
'-abc')
echo "-abc option"
;;
'-h'|'-help')
echo "help option"
;;
*)
echo "usage: $0 {start|stop}"
;;
esac
echo "--------------------------------------------"
shell=tcsh
case $shell in
*csh)
echo "C-shell style shells are not acceptable"
;;
*zsh)
echo " zsh is not acceptable"
;;
esac
---------------------------------------------------------------------
10. 一個 for 的簡單例子
#!/bin/sh
for d in /tmp /usr/tmp /tmp/tmp ; do
echo $d
done
---------------------------------------------------------------------
11. 同樣的 for , 以 *.sh 代替
for file in *.sh ; do
wc -l $file
done
---------------------------------------------------------------------
12. 取得使用者回答的範例
echo "fsck all disks? [y] \c"
read ans
#echo $ans
if [ $ans = 'y' -o $ans = 'Y' ] ; then
echo "Yes"
else
echo "no.."
fi
---------------------------------------------------------------------
13. 迴圈 while ,這個例子會印出 1 2 3 4 5
#!/bin/sh
i=1
while [ $i -le 5 ]; do
echo -n $i
i=`expr $i + 1`
done
---------------------------------------------------------------------
14. 一個字串連結的例子 $a$b$c = cmd , 又 cmd = date
#!/bin/sh
a=c; b=m; c=d; cmd=date
eval $`echo $a$b$c`
---------------------------------------------------------------------
15. 這個例子會列出目前目錄下的子目錄名稱
#!/bin/sh
# usage: process sub-directory
dir=`pwd`
for i in *; do
if [ -d $dir/$i ]
#if test -d $dir/$i
then
cd $dir/$i
# while (echo -n "$i: (waiting command") ; read x; do
# eval $x
# done
echo $i
cd ..
fi
done
---------------------------------------------------------------------
16. 更改檔名 *.html -> *.htm
#!/bin/sh
for i in *.html ; do
echo $i
mv $i `basename $i .html`.htm
done
----------------------------------------------------------------------
17. 更改檔名 *.htm -> *.html
#!/bin/sh
for i in *.htm ; do
echo $i
mv $i `basename $i .htm`.html
done
----------------------------------------------------------------------
18. 一個抓下來的片段
while [ $# != 0 ]
do
case $1 in
-v) doversion=1;;
-B) BASEDIR=$2; shift;;
-l) LOGDIR=$2; shift;;
-w) WORKDIR=$2; shift;;
-B) BINDIR=$2; shift;;
-c) RCFILE=$2; shift;;
-e) EXPLAINREPORT=I;;
-E) EXPLAINREPORT=Y;;
-S) SERVERCHECK=Y;;
-O) OS=$2; shift;;
-A) ARCH=$2; shift;;
-R) REV=$2; shift;;
-t) Tiger_TESTMODE=Y;;
*) echo "--ERROR-- [con006e] Unknown option $1";;
esac
shift;
done
----------------------------------------------------------------------
19. 一個實際設定 Solaris 2.x Ftp server 的 shell script
#!/bin/sh
# script to setup SunOS 5.3 anonymous ftp area
#
#set OS_VERSION = `uname -r`
# handle the optional command line argument
case $# in
# the default location for the anon ftp comes from the passwd file
0) ftphome="`grep '^ftp:' /etc/passwd | cut -d: -f6`"
;;
1) if [ "$1" = "start" ]; then
ftphome="`grep '^ftp:' /etc/passwd | cut -d: -f6`"
else
ftphome=$1
fi
;;
*) echo "Usage: $0 [anon-ftp-root]"
exit 1
;;
esac
if [ -z "${ftphome}" ]; then
echo "$0: ftphome must be non-null"
exit 2
fi
# This script assumes that ftphome is neither / nor /usr so ...
if [ "${ftphome}" = "/" -o "${ftphome}" = "/usr" ]; then
echo "$0: ftphome must not be / or /usr"
exit 2
fi
# If ftphome does not exist but parent does, create ftphome
if [ ! -d ${ftphome} ]; then
# lack of -p below is intentional
mkdir ${ftphome}
fi
echo Setting up anonymous ftp area ${ftphome} for SunOS `uname -r`
#echo Setting up anonymous ftp area ${ftphome} for SunOS $OS_VERSION
# Ensure that the /usr/bin directory exists
if [ ! -d ${ftphome}/usr/bin ]; then
mkdir -p ${ftphome}/usr/bin
fi
cp /usr/bin/ls ${ftphome}/usr/bin
chmod 111 ${ftphome}/usr/bin/ls
# Now set the ownership and modes to match the man page
chown root ${ftphome}/usr/bin
chmod 555 ${ftphome}/usr/bin
# this may not be the right thing to do
# but we need the bin -> usr/bin link
if [ -r ${ftphome}/bin ]; then
mv -f ${ftphome}/bin ${ftphome}/Obin
fi
ln -s usr/bin ${ftphome}
# Ensure that the /usr/lib and /etc directories exist
if [ ! -d ${ftphome}/usr/lib ]; then
mkdir -p ${ftphome}/usr/lib
fi
if [ ! -d ${ftphome}/etc ]; then
mkdir -p ${ftphome}/etc
fi
#Most of the following are needed for basic operation, except
#for libnsl.so, nss_nis.so, libsocket.so, and straddr.so which are
#needed to resolve NIS names.
cp /usr/lib/ld.so /usr/lib/ld.so.1 ${ftphome}/usr/lib
for lib in libc libdl libintl libw libnsl libsocket \
nss_nis nss_nisplus nss_dns nss_files
do
cp /usr/lib/${lib}.so.1 ${ftphome}/usr/lib
rm -f ${ftphome}/usr/lib/${lib}.so
ln -s ./${lib}.so.1 ${ftphome}/usr/lib/${lib}.so
done
cp /usr/lib/straddr.so.2 ${ftphome}/usr/lib
rm -f ${ftphome}/usr/lib/straddr.so
ln -s ./straddr.so.2 ${ftphome}/usr/lib/straddr.so
cp /etc/passwd /etc/group /etc/netconfig ${ftphome}/etc
chmod 555 ${ftphome}/usr/lib/*
chmod 444 ${ftphome}/etc/*
# Now set the ownership and modes
chown root ${ftphome}/usr/lib ${ftphome}/etc
chmod 555 ${ftphome}/usr/lib ${ftphome}/etc
# Ensure that the /dev directory exists
if [ ! -d ${ftphome}/dev ]; then
mkdir -p ${ftphome}/dev
fi
# make device nodes. ticotsord and udp are necessary for
# 'ls' to resolve NIS names.
prefix="/devices/pseudo/mm@0:"
for device in zero
do
line=`ls -l ${prefix}${device} | sed -e 's/,//'`
major=`echo $line | awk '{print $5}'`
minor=`echo $line | awk '{print $6}'`
rm -f ${ftphome}/dev/${device}
mknod ${ftphome}/dev/${device} c ${major} ${minor}
done
prefix="/devices/pseudo/clone@0:"
for device in tcp udp ticotsord
do
line=`ls -l ${prefix}${device} | sed -e 's/,//'`
major=`echo $line | awk '{print $5}'`
minor=`echo $line | awk '{print $6}'`
rm -f ${ftphome}/dev/${device}
mknod ${ftphome}/dev/${device} c ${major} ${minor}
done
chmod 666 ${ftphome}/dev/*
## Now set the ownership and modes
chown root ${ftphome}/dev
chmod 555 ${ftphome}/dev
if [ ! -d ${ftphome}/pub ]; then
mkdir -p ${ftphome}/pub
fi
chown ftp ${ftphome}/pub
chmod 777 ${ftphome}/pub
----------------------------------------------------------------------
20. eval 和 exec 的不同
eval 只是去執行後面的命令,但是 exec 會執行該命令後跳出 shell
看例子就知道
#!/bin/sh
echo "Input command : \n"
read cmd
eval $cmd
# 底下會執行
echo "You can see this line ..after executing $cmd"
#!/bin/sh
echo "Input command : \n"
read cmd
exec $cmd
# 底下不會執行
echo "You can not see this line ..after executing $cmd"
----------------------------------------------------------------------
21. 字串連接, 如下,最後會印出 /etc/yp
yproot_dir=/etc
def_dom=yp
domain_dir="$yproot_dir""/""$def_dom"
echo $domain_dir
----------------------------------------------------------------------
22. case , if , function 綜合用法
#!/bin/sh
killproc() { # kill the named process(es)
pid=`ps -e | grep $1 | sed -e 's/^ *//' -e 's/ .*//'`
# 請特別注意 $pid 和 "$pid" 居然不同,我也不懂
#if [ $pid = "" ]; then
if [ "$pid" = "" ]; then
echo "Can't find process \"$1\" "; exit 2
fi
echo "kill the process \"$1\" with PID $pid"; kill $pid 2> /dev/null
}
case $# in
1)
killproc $1
;;
*)
echo "usage: $0 daemon-name"
;;
esac
----------------------------------------------------------------------
23. 將大寫檔名改成小寫檔名
tr string1 string2 會將 standard input內所對應到的 string1
都以 string2 取代
for file in *; do
mv $file `echo $file | tr '[A-Z]' '[a-z]'`
done
加上一點變化,只處理大寫的檔名,不過執行過後,第二次會有問題,還不是很懂
for file in [A-Z]*; do
echo "process $file"
mv $file `echo $file | tr '[A-Z]' '[a-z]'`
done
----------------------------------------------------------------------
24. case 的變化,一個轉換西元到民國的範例
[0-9][0-9] 表示有兩個存在的數,
[0-9][0-9][0-9][0-9] 表示有四個存在的數
當然也可以 [a-Z] [a-A]等等的變化了
不懂的地方是 echo 1>&2 這個敘述
echo "Input year : \c"
read year
case "$year" in
[0-9][0-9])
year=19${year}
years=`expr $year - 1911`
;;
[0-9][0-9][0-9][0-9])
years=`expr $year - 1911`
;;
*)
echo 1>&2 Year \"$year\" out of range ...
exit 127
;;
esac
echo "$year 是民國 $years"
----------------------------------------------------------------------
25. if 內有兩個判斷式的寫法
echo "Input year : \c"
read year
if [ $year -lt 1901 -o $year -gt 2099 ]; then
echo 1>&2 Year \"$year\" out of range
exit 127
fi
----------------------------------------------------------------------
26. 處理一些多選一的字元
read next
case "$next" in
*[A-Za-z]*) echo "a-z A-Z"
;;
*[0-9]*) echo "0-9"
;;
*) echo "others.."
esac
----------------------------------------------------------------------
27. 7-Apr, 2003 新增
#!/bin/sh
# absolutly path condition
if [ -z `echo $1 | awk -F / {'print $1'}` ]; then
if [ -x $1 ]; then
echo "Yes, $1 is executable"
fi
# others
else
for foo in `echo $PATH | sed -e 's/:/ /g'` ; do
# echo "searching $foo directory"
if [ -x $foo/$1 ]; then
echo "--> $foo/$1 is executable"
exit
fi
done
fi
轉貼自此
SH 教學
1. 一個簡單的例子
#!/bin/sh
echo "** Non-root UID=0 or GID=0 accounts:"
grep ':00*:' /etc/passwd | \
awk -F: 'BEGIN {n=0}
$1 != "root" {print $0 ; n=1}
END {if (n==0) print "None found."}'
---------------------------------------------------------------------
2. shift 的用法,及一些特殊符號的意義
#!/bin/sh
echo $1 $2 $3
echo $#
# will be 4
shift
echo $1 $2 $3
echo $#
# will be 3
# "$*" = "$1 $2 $3....$n"
echo $*
# "$@" = "$1" "$2" "$3" "$4" ..."$n"
echo $@
#
# $? Exit status of previous command
# $$ PID of this shell's process
# $! PID of the most recently started backgroup job
echo $?
echo $$
echo $!
---------------------------------------------------------------------
3. 變數設定的方法,及特殊設定
#!/bin/sh
item=aaaa
item1=bbbb
echo ${item}1 $item1
#!/bin/sh
name=rache1
echo ${name-tatiana}
# show rache1
echo ${name2-tatiana}
# show tatiana
echo ${name=tatiana}
# show rache1
echo ${name2=tatiana}; echo $name2
# show tatiana
# tatiana
dir=${name5-`pwd`}; echo $dir
---------------------------------------------------------------------
4. if 的用法,有很多比較方法 -s, -r, -f, -d .. 請看 man if or man test
例如:底下兩敘述是相同的
if test -z "$var"; then ...
if [ -z "$var" ]; then ...
#!/bin/sh
# in sh , echo -n 只有當 /usr/ucb 路徑在 /usr/bin 之前才有用
if [ -f /usr/bin/ls ]; then
echo -n " ls command found "
fi
#!/bin/sh
strings /vmunix | grep UNIX > /tmp/motd
head -1 /etc/motd | grep UNIX > /tmp/th
if [ -z /tmp/th ]
then
cat /etc/motd >> /tmp/motd
else
tail +2 /etc/motd >> /tmp/motd
fi
mv /tmp/motd /etc/motd
---------------------------------------------------------------------
5. 再看一個加上變化的 if
#!/bin/sh
set `who -r`
if [ $9 = "S" ]; then
echo "The system is coming up. Be patient."
elif [ $7 = "2" ]; then
echo "Changing to state 2."
else
echo "Changing to state 3."
fi
底下是更多的 if 的例子
if [ $9 = "S" ]
if [ -s /etc/ptmp ]
if [$# -lt 4 ]
if [ ! -f /etc/.fscksk ]
if [ $? -eq 0 ]
if [ $? -ne 0 ]
if test -z "$var"
if [ -z "$var" ]
---------------------------------------------------------------------
6. 還是 if 的例子
#!/bin/sh
pid=`/bin/ps -e | grep 'lpsched$' | sed -e 's/^ *//' -e 's/ .*//'`
echo ${pid}
if [ "${pid}" != "" ]
then
echo $pid
/bin/kill ${pid}
fi
if [ -r /fastboot ]; then
echo "skip the fsck"
else
echo "do the fsck"
fi
if [ -d /etc/rc0.d ]
then
echo "run the K files"
fi
if [ -x /etc/inetd ]
then
echo "inetd"
fi
if [ "${BOOT}" = "yes" -a -d /etc/rc0.d ]
then
echo "run /etc/rc0.d"
fi
---------------------------------------------------------------------
7. while 的語法,與讀進參數
#!/bin/sh
cat /etc/vfstab |
while read DEVICE MOUNT_DIR READONLY FS DUMMY1 DUMMY2
do
echo "$DEVICE, $MOUNT_DIR, $READONLY, $FS, $DUMMY1, $DUMMY2"
done
---------------------------------------------------------------------
8. case 的用法,和 csh 真的差很多
$? 是執行命令後的 return status , 0: succeed , 1: false
#!/bin/sh
#/etc/fsck -p > /dev/console
ls / > /dev/null
case $? in
0)
echo "return 0"
;;
2)
exit 1
;;
4)
echo "return 4"
;;
*)
echo "Unknown error in reboot" > /dev/console
exit 1
;;
esac
---------------------------------------------------------------------
9. 再一個 case 的範例
# can get the same result when "$1" or $1
#case $1 in
case "$1" in
'start')
echo "start"
;;
'stop')
echo "stop"
;;
'-abc')
echo "-abc option"
;;
'-h'|'-help')
echo "help option"
;;
*)
echo "usage: $0 {start|stop}"
;;
esac
echo "--------------------------------------------"
shell=tcsh
case $shell in
*csh)
echo "C-shell style shells are not acceptable"
;;
*zsh)
echo " zsh is not acceptable"
;;
esac
---------------------------------------------------------------------
10. 一個 for 的簡單例子
#!/bin/sh
for d in /tmp /usr/tmp /tmp/tmp ; do
echo $d
done
---------------------------------------------------------------------
11. 同樣的 for , 以 *.sh 代替
for file in *.sh ; do
wc -l $file
done
---------------------------------------------------------------------
12. 取得使用者回答的範例
echo "fsck all disks? [y] \c"
read ans
#echo $ans
if [ $ans = 'y' -o $ans = 'Y' ] ; then
echo "Yes"
else
echo "no.."
fi
---------------------------------------------------------------------
13. 迴圈 while ,這個例子會印出 1 2 3 4 5
#!/bin/sh
i=1
while [ $i -le 5 ]; do
echo -n $i
i=`expr $i + 1`
done
---------------------------------------------------------------------
14. 一個字串連結的例子 $a$b$c = cmd , 又 cmd = date
#!/bin/sh
a=c; b=m; c=d; cmd=date
eval $`echo $a$b$c`
---------------------------------------------------------------------
15. 這個例子會列出目前目錄下的子目錄名稱
#!/bin/sh
# usage: process sub-directory
dir=`pwd`
for i in *; do
if [ -d $dir/$i ]
#if test -d $dir/$i
then
cd $dir/$i
# while (echo -n "$i: (waiting command") ; read x; do
# eval $x
# done
echo $i
cd ..
fi
done
---------------------------------------------------------------------
16. 更改檔名 *.html -> *.htm
#!/bin/sh
for i in *.html ; do
echo $i
mv $i `basename $i .html`.htm
done
----------------------------------------------------------------------
17. 更改檔名 *.htm -> *.html
#!/bin/sh
for i in *.htm ; do
echo $i
mv $i `basename $i .htm`.html
done
----------------------------------------------------------------------
18. 一個抓下來的片段
while [ $# != 0 ]
do
case $1 in
-v) doversion=1;;
-B) BASEDIR=$2; shift;;
-l) LOGDIR=$2; shift;;
-w) WORKDIR=$2; shift;;
-B) BINDIR=$2; shift;;
-c) RCFILE=$2; shift;;
-e) EXPLAINREPORT=I;;
-E) EXPLAINREPORT=Y;;
-S) SERVERCHECK=Y;;
-O) OS=$2; shift;;
-A) ARCH=$2; shift;;
-R) REV=$2; shift;;
-t) Tiger_TESTMODE=Y;;
*) echo "--ERROR-- [con006e] Unknown option $1";;
esac
shift;
done
----------------------------------------------------------------------
19. 一個實際設定 Solaris 2.x Ftp server 的 shell script
#!/bin/sh
# script to setup SunOS 5.3 anonymous ftp area
#
#set OS_VERSION = `uname -r`
# handle the optional command line argument
case $# in
# the default location for the anon ftp comes from the passwd file
0) ftphome="`grep '^ftp:' /etc/passwd | cut -d: -f6`"
;;
1) if [ "$1" = "start" ]; then
ftphome="`grep '^ftp:' /etc/passwd | cut -d: -f6`"
else
ftphome=$1
fi
;;
*) echo "Usage: $0 [anon-ftp-root]"
exit 1
;;
esac
if [ -z "${ftphome}" ]; then
echo "$0: ftphome must be non-null"
exit 2
fi
# This script assumes that ftphome is neither / nor /usr so ...
if [ "${ftphome}" = "/" -o "${ftphome}" = "/usr" ]; then
echo "$0: ftphome must not be / or /usr"
exit 2
fi
# If ftphome does not exist but parent does, create ftphome
if [ ! -d ${ftphome} ]; then
# lack of -p below is intentional
mkdir ${ftphome}
fi
echo Setting up anonymous ftp area ${ftphome} for SunOS `uname -r`
#echo Setting up anonymous ftp area ${ftphome} for SunOS $OS_VERSION
# Ensure that the /usr/bin directory exists
if [ ! -d ${ftphome}/usr/bin ]; then
mkdir -p ${ftphome}/usr/bin
fi
cp /usr/bin/ls ${ftphome}/usr/bin
chmod 111 ${ftphome}/usr/bin/ls
# Now set the ownership and modes to match the man page
chown root ${ftphome}/usr/bin
chmod 555 ${ftphome}/usr/bin
# this may not be the right thing to do
# but we need the bin -> usr/bin link
if [ -r ${ftphome}/bin ]; then
mv -f ${ftphome}/bin ${ftphome}/Obin
fi
ln -s usr/bin ${ftphome}
# Ensure that the /usr/lib and /etc directories exist
if [ ! -d ${ftphome}/usr/lib ]; then
mkdir -p ${ftphome}/usr/lib
fi
if [ ! -d ${ftphome}/etc ]; then
mkdir -p ${ftphome}/etc
fi
#Most of the following are needed for basic operation, except
#for libnsl.so, nss_nis.so, libsocket.so, and straddr.so which are
#needed to resolve NIS names.
cp /usr/lib/ld.so /usr/lib/ld.so.1 ${ftphome}/usr/lib
for lib in libc libdl libintl libw libnsl libsocket \
nss_nis nss_nisplus nss_dns nss_files
do
cp /usr/lib/${lib}.so.1 ${ftphome}/usr/lib
rm -f ${ftphome}/usr/lib/${lib}.so
ln -s ./${lib}.so.1 ${ftphome}/usr/lib/${lib}.so
done
cp /usr/lib/straddr.so.2 ${ftphome}/usr/lib
rm -f ${ftphome}/usr/lib/straddr.so
ln -s ./straddr.so.2 ${ftphome}/usr/lib/straddr.so
cp /etc/passwd /etc/group /etc/netconfig ${ftphome}/etc
chmod 555 ${ftphome}/usr/lib/*
chmod 444 ${ftphome}/etc/*
# Now set the ownership and modes
chown root ${ftphome}/usr/lib ${ftphome}/etc
chmod 555 ${ftphome}/usr/lib ${ftphome}/etc
# Ensure that the /dev directory exists
if [ ! -d ${ftphome}/dev ]; then
mkdir -p ${ftphome}/dev
fi
# make device nodes. ticotsord and udp are necessary for
# 'ls' to resolve NIS names.
prefix="/devices/pseudo/mm@0:"
for device in zero
do
line=`ls -l ${prefix}${device} | sed -e 's/,//'`
major=`echo $line | awk '{print $5}'`
minor=`echo $line | awk '{print $6}'`
rm -f ${ftphome}/dev/${device}
mknod ${ftphome}/dev/${device} c ${major} ${minor}
done
prefix="/devices/pseudo/clone@0:"
for device in tcp udp ticotsord
do
line=`ls -l ${prefix}${device} | sed -e 's/,//'`
major=`echo $line | awk '{print $5}'`
minor=`echo $line | awk '{print $6}'`
rm -f ${ftphome}/dev/${device}
mknod ${ftphome}/dev/${device} c ${major} ${minor}
done
chmod 666 ${ftphome}/dev/*
## Now set the ownership and modes
chown root ${ftphome}/dev
chmod 555 ${ftphome}/dev
if [ ! -d ${ftphome}/pub ]; then
mkdir -p ${ftphome}/pub
fi
chown ftp ${ftphome}/pub
chmod 777 ${ftphome}/pub
----------------------------------------------------------------------
20. eval 和 exec 的不同
eval 只是去執行後面的命令,但是 exec 會執行該命令後跳出 shell
看例子就知道
#!/bin/sh
echo "Input command : \n"
read cmd
eval $cmd
# 底下會執行
echo "You can see this line ..after executing $cmd"
#!/bin/sh
echo "Input command : \n"
read cmd
exec $cmd
# 底下不會執行
echo "You can not see this line ..after executing $cmd"
----------------------------------------------------------------------
21. 字串連接, 如下,最後會印出 /etc/yp
yproot_dir=/etc
def_dom=yp
domain_dir="$yproot_dir""/""$def_dom"
echo $domain_dir
----------------------------------------------------------------------
22. case , if , function 綜合用法
#!/bin/sh
killproc() { # kill the named process(es)
pid=`ps -e | grep $1 | sed -e 's/^ *//' -e 's/ .*//'`
# 請特別注意 $pid 和 "$pid" 居然不同,我也不懂
#if [ $pid = "" ]; then
if [ "$pid" = "" ]; then
echo "Can't find process \"$1\" "; exit 2
fi
echo "kill the process \"$1\" with PID $pid"; kill $pid 2> /dev/null
}
case $# in
1)
killproc $1
;;
*)
echo "usage: $0 daemon-name"
;;
esac
----------------------------------------------------------------------
23. 將大寫檔名改成小寫檔名
tr string1 string2 會將 standard input內所對應到的 string1
都以 string2 取代
for file in *; do
mv $file `echo $file | tr '[A-Z]' '[a-z]'`
done
加上一點變化,只處理大寫的檔名,不過執行過後,第二次會有問題,還不是很懂
for file in [A-Z]*; do
echo "process $file"
mv $file `echo $file | tr '[A-Z]' '[a-z]'`
done
----------------------------------------------------------------------
24. case 的變化,一個轉換西元到民國的範例
[0-9][0-9] 表示有兩個存在的數,
[0-9][0-9][0-9][0-9] 表示有四個存在的數
當然也可以 [a-Z] [a-A]等等的變化了
不懂的地方是 echo 1>&2 這個敘述
echo "Input year : \c"
read year
case "$year" in
[0-9][0-9])
year=19${year}
years=`expr $year - 1911`
;;
[0-9][0-9][0-9][0-9])
years=`expr $year - 1911`
;;
*)
echo 1>&2 Year \"$year\" out of range ...
exit 127
;;
esac
echo "$year 是民國 $years"
----------------------------------------------------------------------
25. if 內有兩個判斷式的寫法
echo "Input year : \c"
read year
if [ $year -lt 1901 -o $year -gt 2099 ]; then
echo 1>&2 Year \"$year\" out of range
exit 127
fi
----------------------------------------------------------------------
26. 處理一些多選一的字元
read next
case "$next" in
*[A-Za-z]*) echo "a-z A-Z"
;;
*[0-9]*) echo "0-9"
;;
*) echo "others.."
esac
----------------------------------------------------------------------
27. 7-Apr, 2003 新增
#!/bin/sh
# absolutly path condition
if [ -z `echo $1 | awk -F / {'print $1'}` ]; then
if [ -x $1 ]; then
echo "Yes, $1 is executable"
fi
# others
else
for foo in `echo $PATH | sed -e 's/:/ /g'` ; do
# echo "searching $foo directory"
if [ -x $foo/$1 ]; then
echo "--> $foo/$1 is executable"
exit
fi
done
fi
2008年7月18日 星期五
SQL語法
SQL語法
資料來源
初階
查詢:列出住在比較溫暖地區的顧客
SELECT CUSTOMER_NAME,CITY
FROM CUSTOMER_V
WHERE CITY IN (’台南市’,'高雄市’,'屏東市’)
查詢:針對CUSTOMER_V中地址在’台南市’,'高雄市’,'屏東市’中的所有顧客,列出他們的名稱.所在城市.而且顯示的順序是依據城市的字母順序,每個城市內的顧客亦照字母順序排列(預設為升冪)
SELECT CUSTOMER_NAME,CITY
FROM CUSTOMER_V
WHERE CITY IN (’台南市’,'高雄市’,'屏東市’)
ORDER BY CITY , CUSTOMER_NAME
‘排列順序是依據所列出的欄位順序來決定的;若要由高到低排列,則在排序欄位後面加上DESC關鍵字
查詢:計算我們出貨到每一州的顧客數目
SELECT STATE,COUNT(STATE)
FROM CUSTOMER_V
GROUP BY STATE;
查詢:計算我們出貨到每個城市的顧客數目,請依據州來列出這些城市
SELECT STATE,CITY,COUNT(CITY)
FROM CUSTOMER_V
GROUP BY STATE,CITY;
查詢:計算我們出貨到每個州的顧客數目,請依據州來列出這些顧客,並找出有一個顧客以上的州
SELECT STATE,COUNT(STATE)
FROM CUSTOMER_V
GROUP BY STATE
HAVING COUNT(STATE) > 1
查詢:針對平均定價少於$750的外裝產品,列出這些外裝產品的外裝與平均定價.
SELECT PRODUCT_FINISH,AVG(STANDARD_PRICE)
FROM PRODUCT_V
WHERE PRODUCT_FINISH IN (’運動鞋’,'跑步鞋’,'淑女鞋’)
GROUP BY PRODUCT_FINISH
HAVING AVG(STANDARD_PRICE)
‘注意:這六個關鍵字若要同時出現,必須以這種順序呈現….(這是語法順序)
(這是處理順序)
FROM 指定涉及的表格(TABLE)
WHERE 找出符合指定條件的所有列(ROW)
GROUP BY 根據指定欄位的值組織列(ROW)
HAVING 找出符合指定條件的所有群組
SELECT 指定要顯示給使用者看的欄位(COLUMN)
ORDER BY 排列資料列(ROW)的順序
進階
等值合併
查詢:下訂單的所有顧客名稱
SELECT CUSTOMER_T.CUSTOMER_ID,ORDER_T.CUSTOMER_ID,CUSTOMER_NAME,ORDER_ID
FROM CUSTOMER_T,ORDER_T
WHERE CUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID;
自然合併
查詢:對於每下訂單的顧客名稱,查出他的姓名與訂單號碼為何?
SELECT CUSOTMER_T.ID,CUSTOMER_T.NAME,ORDER_T.ID
FROM CUSTOMER_T,ORDER_T
WHERE CUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID
外部合併
查詢:為CUSTOMER表格中的所有顧客,列出他們的顧客姓名.識別碼及訂單號碼.即使沒有訂單的顧客,也請列出他們的顧客識別碼與姓名
SELECT CUSTOMER_T.NAME,CUSTOMER_T.CUSTOMER_ID,ORDER_ID
FROM CUSTOMER_T LEFT OUTER JOIN ORDER_T
WHERE CUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID
查詢:為ORDER表格中的所有訂單,列出他們的顧客姓名.識別碼及訂單號碼.即使沒有顧客姓名,也要加入訂單號碼哦(這個查詢最主要的目的在於確認參考的完整性)
SELECT CUSTOMER_T.NAME,CUSTOMER_T.CUSTOMER_T.CUSTOMER_ID,ORDER_ID
FROM CUSTOMER_T RIGHT OUTER JOIN ORDER_T ON CUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID;
聯集合併
UNION JOIN = FULL OUTER JOIN
UNION JOIN <> UNION
UNION JOIN 的結果是每個合併
合併兩個表格的兩種方式:
利用合併技巧,在where子句中,加入兩個表格共同的欄位等式
利用子查詢或稱為巢狀查詢
子查詢
當要擷取與顯示來自數個關聯表的資料,並且未必是巢狀關係時,可以使用合併技巧.
查詢:對#1008下訂單的顧客姓名與地址(使用合併技巧)
SELECT CUSTOMER_NAME,CUSTOMER_ADDRESS,CITY,STATE,POSTAL_CODE
FROM CUSTOMER_T,ORDER_T
WHERE CUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID AND ORDER_T.ORDER_ID = 1008;
查詢:對#1008下訂單的顧客姓名與地址(使用子查詢)
SELECT CUSTOMER_NAME,CUSTOMER_ADDRESS,CITY,STATE,POSTAL_CODE
FROM CUSTOMER_T
WHERE CUSTOMER_T.CUSTOMER_ID = (SELECT ORDER_T.CUSTOMER_ID FROM ORDER_T WHERE ORDER_T.ORDER_ID = 100
注意:若要將子查詢的結果顯示在最終結果中,就得使用合併技巧,因為子查詢資料不能出現在最終的結果中(VIP)
查詢:哪些顧客下過訂單?(使用子查詢-傳回多筆值)
SELECT CUSTOMER_NAME
FROM CUSTOMER_T
WHERE CUSTOMER_T.CUSTOMER_ID IN
( SELECT DISTINCT CUSTOMER_ID FROM ORDER_T)
查詢:哪些顧客未訂購過電腦桌?(使用子查詢-傳回多筆值)
SELECT CUSTOMER_NAME
FROM CUSTOMER_T
WHERE CUSTOMER_ID NOT IN
(SELECT CUSTOMER_ID
FROM ORDER_T,ORDER_LINE_T,PRODUCT_T
WHERE ORDER_T.ORDER_ID = ORDER_LINE_T.ORDER_ID
AND ORDER_LINE_T.PRODUCT_ID = PRODUCT_T.PRODUCT_ID
AND PRODUCT_NAME = ‘COMPUTER DEST’ );
查詢:所有包含天然梣木傢具的訂單編號
SELECT ORDER_ID
FROM ORDER_LINE_T
WHERE EXITS
( SELECT *
FROM PRODUCT_T
WHERE PRODUCT_ID = ORDER_LINE_T.PRODUCT_ID
AND PRODUCT_FINISH = ‘NATURAL ASH’ );
在子查詢中使用EXITS時,它在意的是是否有符合條件的資料即可,
並不會利用子查詢的結果來篩選出母查詢(外層)最終的結果
還有啊,相關聯子查詢的處理順序會是從外到內,而一般的子查詢則是從內到外…有點不一樣
查詢:列出最高單價產品的明細(這是高級技巧)
SELECT PRODUCT_NAME,PRODUCT_FINISH,UNIT_PRICE
FROM PRODUCT_T PA
WHERE UNIT_PRICE > ALL
( SELECT UNIT_PRICE
FROM PRODUCT_T PB
WHERE PB.PRODUCT_ID PA.PRODUCT_ID )
查詢:哪個產品的標準價格高過平均標準價格?
SELECT PRODUCT_DESCRIPTION,STANDARD_PRICE,AVGPRICE
FROM ( SELECT AVG(STANDARD_PRICE) AVGPRICE FROM PRODUCT_T ),PRODUCT_T
WHERE STANDARD_PRICE > AVGPRICE ;
資料來源
初階
查詢:列出住在比較溫暖地區的顧客
SELECT CUSTOMER_NAME,CITY
FROM CUSTOMER_V
WHERE CITY IN (’台南市’,'高雄市’,'屏東市’)
查詢:針對CUSTOMER_V中地址在’台南市’,'高雄市’,'屏東市’中的所有顧客,列出他們的名稱.所在城市.而且顯示的順序是依據城市的字母順序,每個城市內的顧客亦照字母順序排列(預設為升冪)
SELECT CUSTOMER_NAME,CITY
FROM CUSTOMER_V
WHERE CITY IN (’台南市’,'高雄市’,'屏東市’)
ORDER BY CITY , CUSTOMER_NAME
‘排列順序是依據所列出的欄位順序來決定的;若要由高到低排列,則在排序欄位後面加上DESC關鍵字
查詢:計算我們出貨到每一州的顧客數目
SELECT STATE,COUNT(STATE)
FROM CUSTOMER_V
GROUP BY STATE;
查詢:計算我們出貨到每個城市的顧客數目,請依據州來列出這些城市
SELECT STATE,CITY,COUNT(CITY)
FROM CUSTOMER_V
GROUP BY STATE,CITY;
查詢:計算我們出貨到每個州的顧客數目,請依據州來列出這些顧客,並找出有一個顧客以上的州
SELECT STATE,COUNT(STATE)
FROM CUSTOMER_V
GROUP BY STATE
HAVING COUNT(STATE) > 1
查詢:針對平均定價少於$750的外裝產品,列出這些外裝產品的外裝與平均定價.
SELECT PRODUCT_FINISH,AVG(STANDARD_PRICE)
FROM PRODUCT_V
WHERE PRODUCT_FINISH IN (’運動鞋’,'跑步鞋’,'淑女鞋’)
GROUP BY PRODUCT_FINISH
HAVING AVG(STANDARD_PRICE)
‘注意:這六個關鍵字若要同時出現,必須以這種順序呈現….(這是語法順序)
(這是處理順序)
FROM 指定涉及的表格(TABLE)
WHERE 找出符合指定條件的所有列(ROW)
GROUP BY 根據指定欄位的值組織列(ROW)
HAVING 找出符合指定條件的所有群組
SELECT 指定要顯示給使用者看的欄位(COLUMN)
ORDER BY 排列資料列(ROW)的順序
進階
等值合併
查詢:下訂單的所有顧客名稱
SELECT CUSTOMER_T.CUSTOMER_ID,ORDER_T.CUSTOMER_ID,CUSTOMER_NAME,ORDER_ID
FROM CUSTOMER_T,ORDER_T
WHERE CUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID;
自然合併
查詢:對於每下訂單的顧客名稱,查出他的姓名與訂單號碼為何?
SELECT CUSOTMER_T.ID,CUSTOMER_T.NAME,ORDER_T.ID
FROM CUSTOMER_T,ORDER_T
WHERE CUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID
外部合併
查詢:為CUSTOMER表格中的所有顧客,列出他們的顧客姓名.識別碼及訂單號碼.即使沒有訂單的顧客,也請列出他們的顧客識別碼與姓名
SELECT CUSTOMER_T.NAME,CUSTOMER_T.CUSTOMER_ID,ORDER_ID
FROM CUSTOMER_T LEFT OUTER JOIN ORDER_T
WHERE CUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID
查詢:為ORDER表格中的所有訂單,列出他們的顧客姓名.識別碼及訂單號碼.即使沒有顧客姓名,也要加入訂單號碼哦(這個查詢最主要的目的在於確認參考的完整性)
SELECT CUSTOMER_T.NAME,CUSTOMER_T.CUSTOMER_T.CUSTOMER_ID,ORDER_ID
FROM CUSTOMER_T RIGHT OUTER JOIN ORDER_T ON CUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID;
聯集合併
UNION JOIN = FULL OUTER JOIN
UNION JOIN <> UNION
UNION JOIN 的結果是每個合併
合併兩個表格的兩種方式:
利用合併技巧,在where子句中,加入兩個表格共同的欄位等式
利用子查詢或稱為巢狀查詢
子查詢
當要擷取與顯示來自數個關聯表的資料,並且未必是巢狀關係時,可以使用合併技巧.
查詢:對#1008下訂單的顧客姓名與地址(使用合併技巧)
SELECT CUSTOMER_NAME,CUSTOMER_ADDRESS,CITY,STATE,POSTAL_CODE
FROM CUSTOMER_T,ORDER_T
WHERE CUSTOMER_T.CUSTOMER_ID = ORDER_T.CUSTOMER_ID AND ORDER_T.ORDER_ID = 1008;
查詢:對#1008下訂單的顧客姓名與地址(使用子查詢)
SELECT CUSTOMER_NAME,CUSTOMER_ADDRESS,CITY,STATE,POSTAL_CODE
FROM CUSTOMER_T
WHERE CUSTOMER_T.CUSTOMER_ID = (SELECT ORDER_T.CUSTOMER_ID FROM ORDER_T WHERE ORDER_T.ORDER_ID = 100
注意:若要將子查詢的結果顯示在最終結果中,就得使用合併技巧,因為子查詢資料不能出現在最終的結果中(VIP)
查詢:哪些顧客下過訂單?(使用子查詢-傳回多筆值)
SELECT CUSTOMER_NAME
FROM CUSTOMER_T
WHERE CUSTOMER_T.CUSTOMER_ID IN
( SELECT DISTINCT CUSTOMER_ID FROM ORDER_T)
查詢:哪些顧客未訂購過電腦桌?(使用子查詢-傳回多筆值)
SELECT CUSTOMER_NAME
FROM CUSTOMER_T
WHERE CUSTOMER_ID NOT IN
(SELECT CUSTOMER_ID
FROM ORDER_T,ORDER_LINE_T,PRODUCT_T
WHERE ORDER_T.ORDER_ID = ORDER_LINE_T.ORDER_ID
AND ORDER_LINE_T.PRODUCT_ID = PRODUCT_T.PRODUCT_ID
AND PRODUCT_NAME = ‘COMPUTER DEST’ );
查詢:所有包含天然梣木傢具的訂單編號
SELECT ORDER_ID
FROM ORDER_LINE_T
WHERE EXITS
( SELECT *
FROM PRODUCT_T
WHERE PRODUCT_ID = ORDER_LINE_T.PRODUCT_ID
AND PRODUCT_FINISH = ‘NATURAL ASH’ );
在子查詢中使用EXITS時,它在意的是是否有符合條件的資料即可,
並不會利用子查詢的結果來篩選出母查詢(外層)最終的結果
還有啊,相關聯子查詢的處理順序會是從外到內,而一般的子查詢則是從內到外…有點不一樣
查詢:列出最高單價產品的明細(這是高級技巧)
SELECT PRODUCT_NAME,PRODUCT_FINISH,UNIT_PRICE
FROM PRODUCT_T PA
WHERE UNIT_PRICE > ALL
( SELECT UNIT_PRICE
FROM PRODUCT_T PB
WHERE PB.PRODUCT_ID PA.PRODUCT_ID )
查詢:哪個產品的標準價格高過平均標準價格?
SELECT PRODUCT_DESCRIPTION,STANDARD_PRICE,AVGPRICE
FROM ( SELECT AVG(STANDARD_PRICE) AVGPRICE FROM PRODUCT_T ),PRODUCT_T
WHERE STANDARD_PRICE > AVGPRICE ;
RedHat 9.0竟然沒有 mkfs.jffs2這個指令!!!
RedHat 9.0竟然沒有 mkfs.jffs2這個指令!!!
網路上搜尋了一下~
找到了解決方法!!
下載mkfs.jffs2這個binary檔,然後放入/sbin的資料夾裡,即可使用。
mkfs.jffs2下載點
訂閱:
文章 (Atom)