K shell Operators

touch test
vi test编辑脚本
test
#!/bin/ksh
who |wc -l
rmdir test.1
num=2
echo "this is the $num"
Esc退出编辑模式
:wq保存退出
chmod +x test设置可执行权限,这样就可以像shell一样直接执行
db2进入DB2编辑模式
quit退出DB2编辑模式
sh -x test执行test脚本,同时列出变量值
sh -n test不执行脚本只检查语法,会返回语法错误
head -3 1.txt
 tail -3 1.txt
top 查看cpu等的使用率
 sudo find / -name 1.txt 可以使用root权限
cd ~ 直接到home目录
cd
ps -ef
find find file
find  .-name ".csv"
grep
df -g 查看文件系统分区
du -sm * 察看当前目录的文件大小
chmod 改变文件权限
chmod 改变文件权限
topas (aix上面)查看系统状态,io, cup, memory之类的
uname 查看系统名
awk, sed, cut 用来进行文本处理
useradd,groupadd,passwd
wc 统计行数,字符数
vi 文本编辑
who
管道符 |
重定向 >><<
mkdir 创建目录
more,cat 查看文件内容

Table 2-3 Integer Comparison Operators
Operator Returns
n1 == n2 Success if integers n1 and n2 are equal
n1 != n2 Success if integers n1 and n2 are not equal
n1 > n2 Success if integer n1 is greater than integer n2
n1 >= n2 Success if integer n1 is greater than or equal to integer n2
n1 < n2 Success if integer n1 is less than integer n2
n1 <= n2 Success if integer n1 is less than or equal to integer n2

Table 2-4 String Comparison
Operator Returns
-z s1 Success if length of string s1 is zero
-n s1 Success if length of string s1 is non-zero
s1 = s2 Success if strings s1 and s2 are identical
s1 != s2 Success if strings s1 and s2 are not identical
s1 < s2 Success if string s1 comes before string s2 based on their
ASCII values
s1 > s2 Success if string s1 comes after string s2 based on their
ASCII values

Table 2-5 File Enquiry Operators
Operator Returns
-a file Success if file exists
-r file Success if file exists and is readable
-w file Success if file exists and is writable
-x file Success if file exists and is executable
-f file Success if file exists and is a regular file (as opposed to a
directory)
-d file Success if file exists and is a directory
-s file Success if file exists and has a size greater than zero
file1 -nt file2 Success if file1 is newer than file2
file1 -ot file2 Success if file1 is older than file2
file1 -ef file2 Success if file1 is another name for file2


Table A-1 Common Symbols
Symbol Definition
$1, $2,... $9,${10},
${11}...
Positional parameters specified on command line
$0  Name of shell script
$$  Process id (PID) of currently executing shell
$PPID Process ID of parent process to current shell
$# Number of positional parameters
$* All positional parameters
$? Exit status of most recent command
$! Process ID of last background process
${var:-default} ! Value for variable var
! Default value if var not set
${#var} Number of characters in the value of variable var
${#array[*]} Number of elements in array named array

! Semicolon (;)
! Parenthesis (())
! Braces ({}) (or curly braces)

Using the Semicolon
Use the semicolon to join several shell commands on the same line. Each
command executes as if typed on separate lines.
The following example shows how to group two commands with a semicolon.
Example
$ cd /etc ; ls m*
mail.aliases miscd mkfs
mklost+found mknod mkpasswd
mkproto motd mount
$ pwd
/etc

Using Parentheses
Parentheses force the group to execute in a child shell.
The following example shows how to use parenthesis.
Example
$ pwd # where are we?
/usr/users/sam
$ (cd /etc ; ls m*) # run in a child shell
mail.aliases miscd mkfs
mklost+found mknod mkpasswd
mkproto motd mount
$ pwd # child shell cannot influence its parent.
/usr/users/sam

Using Braces
Braces cause the group to execute in the parent shell, but allow redirection of
multiple commands.
$ { cd /etc ; pwd ; ls m* ; } > ~/list
Note the punctuation: Spaces before and after the braces are essential; the trailing
semicolon is also essential.
The following example shows how to use braces.
Example
$ cat ~/list
/etc
mail.aliases miscd mkfs
mklost+found mknod mkpasswd
mkproto motd mount
$ pwd
/etc

Parent and Child Scripts
When you run a shell script. from the KornShell command line, the shell script. is a
“child” and the KornShell command line is the “parent.” A child cannot change
the parent’s environment.
The following example shows how a child cannot change the parent’s working
directory.
Example
$ pwd # what is the current directory?
/usr/users/sam
$ cat show_cron # here is a simple shell script
cd /var/spool/cron/crontabs
ls
$ show_cron # run the shell script
adm cronuucp milt
root sys uucp
$ pwd # why did the directory not change?
/usr/users/sam

Running a Sibling
Using the Dot Command
The following example runs show_cron as a sibling.
Example
$ . show_cron # run the script. as a dot command
adm cronuucp milt
root sys uucp
$ pwd # now the directory changed!
/var/spool/cron/crontabs

The exec Command
The exec command replaces the parent shell with any shell command, as shown
in the following example. The exec command is seldom used.
Example
$ exec show_cron
adm cronuucp milt
root sys uucp
login:

The && Operator
The following example shows how to use the && operator.
Example
cmd1 && cmd2 execute cmd2 only if cmd1 succeeds.
$ who | grep -q sam && print "sam is logged on"
The | | Operator
The following example shows how to use the || operator.
Example
cmd1 || cmd2 execute cmd2 only if cmd1 fails.
$ who | grep -q sam || print "sam NOT logged on"

Using the case Statement
The following code example shows typical usage of case.
Example
case $1 in
[0-9]) print "is a single digit"
;;
dog|cat) print "is name of house pet"
;;
lion) print "is king of the jungle"
;;
[a-z]*) print "begins with lower case"
;;
*) print "none of the above"
;;
esac
----------------------------------------------
while [ $1 ]
do
  case $1 in
  "1") shift; month="2011-01-01";;
  "2") shift; month="2011-02-01";;
  "3") shift; month="2011-03-01";;
  "4") shift; month="2011-04-01";;
  "5") shift; month="2011-05-01";;
  "6") shift; month="2011-06-01";;
  "7") shift; month="2011-07-01";;
  "8") shift; month="2011-08-01";;
  "9") shift; month="2011-09-01";;
  "10") shift; month="2011-10-01";;
  "11") shift; month="2011-11-01";;
  "12") shift; month="2011-12-01";;
  esac

Using the until Statement
The following example shows how to use until in a negative iteration loop.
Example
until [[ $1 = "harry" ]]
do
print "Where is harry? - $1"
shift
done
$ myscript. sam mary harry tom
Where is harry? - sam
Where is harry? - mary

Using the while read Statement
The following is an example of using the while read statement combination
below.
Example
$ cat myscript
while read line
do
print $line
done < file1 > file2
print “That is all.”

By redirecting standard input and standard output on the done line, the KornShell
will gather all input for the while read statement from file1 and will send
all output within the body of the loop to file2.

$ cat file1 # here is the input file
This is the first line of file1.
This is the second line of file1.
This is the third line of file1.

$ myscript. # now we will run the script
That’s all. # notice that this line does not go into file2.

$ cat file2 # here is the output file

This is the first line of file1.
This is the second line of file1.
This is the third line of file1.

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7899089/viewspace-733757/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/7899089/viewspace-733757/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值