Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1502762
  • 博文数量: 289
  • 博客积分: 11086
  • 博客等级: 上将
  • 技术积分: 3291
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-22 17:06
个人简介

徐小玉的博客。

文章分类

全部博文(289)

文章存档

2023年(6)

2022年(1)

2021年(2)

2020年(9)

2019年(9)

2018年(6)

2017年(10)

2016年(10)

2014年(3)

2013年(4)

2011年(12)

2010年(16)

2009年(14)

2008年(119)

2007年(48)

2006年(20)

我的朋友

分类:

2008-11-06 11:25:13

May 30
1:
   echo $SHELL           查看当前用户默认的SHELL
   echo  $0                     查看用户当前的SHELL
May 29
   1:  a 目录下有如下文件:
   ubantu_123.txt   linux.32a.txt    debian-fke.dle.txt 等等前缀相同的N多文件。。。。
   要求将不同前缀的分类移动到,以该前缀命名的目录下(这些目录要在脚本中自动建立!)!!!!
    如:ubantu_123.txt  在./ubantu/ubantu_123.txt         linux.32a.txt     ./linux/linux.32a.txt
   实现:
   代码:for i in `ls *.txt`;do dir=`echo $i|awk -F'[_\.]' '{print $1}'`;mkdir $dir;cp $i ./$dir; done
   2:
    whoami  ==> 显示当前有效用户
who am i ==> 显示login id
#!/bin/sh
#hell2.sh -- 用来向用户问好。
if [ $# -ne 1 ] ;
then
echo "Usage: $0 username";
exit 1;
else
echo "Hello World, Hello $1 !";
fi
这把看懂了么?有点意思吧?下面给你解释一下:
1. [...] 是用来测试。
2. -ne 表示不等于
3. if 用于判断,和then else 联合使用,以 fi 结束
4. $# 表示参数个数
5. $0 表示脚本名称
6. $1 表示第一个参数。

Unix shell by example
Chapter 8
Table 8.3. Bourne Shell Positional Parameters
Positional Parameter What It References
$0 References the name of the script
$# Holds the value of the number of positional parameters
$* Lists all of the positional parameters
$@ Means the same as $*, except when enclosed in double quotes
"$*" Expands to a single argument (e.g., "$1 $2 $3")
"$@" Expands to separate arguments (e.g., "$1" "$2" "$3")
$1 .. $9 References up to nine positional parameters
例子:#!/usr/bin/sh
echo " this script is called $0"
echo " $0  and $1 $2 $3  $4"
echo " the \$*  is  $*"
echo " the number of positional parameters is $#"
结果:
$ greetings.sh tom Mei Yan  lIng
 this script is called greetings.sh
 greetings.sh  and tom Mei Yan  lIng
 the $*  is  tom Mei Yan lIng
 the number of positional parameters is 4

name=${1:?"requires an argument" }   #  需要参数,如果$1存在,,那么就正常执行,否则,提示这样的信息。
   echo Hello $name
String, Integer, and File Testing
Table 8.4. String, Integer, and File Testing
Test Operator Test For
String Test
string1 = string2 String1 is equal to String2 (space surrounding = required)
string1 != string2 String1 is not equal to String2 (space surrounding != required)
string String is not null
–z string Length of string is zero
–n string Length of string is nonzero
  EXAMPLE
test -n $word      or      [ -n $word ]
test tom = sue      or      [ tom = sue ]

Integer Test
int1 –eq int2 Int1 is equal to int2
int1 –ne int2 Int1 is not equal to int2
int1 –gt int2 Int1 is greater than int2
int1 –ge int2 Int1 is greater than or equal to int2
int1 –lt int2 Int1 is less than int2
int1 –le int2 Int1 is less than or equal to int2
Logical Test
expr1 -a expr2 Logical AND
expr1 -o expr2 Logical OR
! expr Logical NOT
File Test
–b filename Block special file
–c filename Character special file
–d filename Directory existence
–f filename Regular file existence and not a directory
–g filename Set-group-ID is set
–k filename Sticky bit is set
–p filename File is a named pipe
–r filename File is readable
–s filename File is nonzero size
–u filename Set-user-ID bit is set
–w filename File is writable
–x filename File is executable
Case语句
case variable in
value1)
    command(s)
    ;;
value2)
    command(s)
    ;;
*)
command(s)
    ;;
Esac
制造菜单
cat << ENDIT
        1) vt 120
        2) wyse50
        3) sun
2 ENDIT
read choice
case "$choice" in
   …
Exec命令
Shift
#!/bin/sh
1    set joe mary tom sam
2    shift
3    echo $*
4    set `date`
5    echo  $*
6    shift 5
7    echo  $*
8    shift 2
 
(The Output)
3    mary tom sam
5    Fri Sep 9 10:00:12 PDT 2004
7    2004
8 cannot shift
在一个程序里调用另一个程序里的函数的方法。(不对)
Function.sh:
#!/usr/bin/sh
increment()
{
  sum=`expr $1 + $2`;
  return $sum;
}
Func_father.sh
#!/usr/bin/sh
. function.sh
increment 4 5
(output)
Chapter 7
?  uname –n
  显示机器名。
   uname –a 
AIX ODP590-1 3 5 00CD7B9F4C00
Chapter 6
? % awk '/Tom/,/Suzanne/' filename
查找文件中 tom 与suzanne中的内容,显示出来。
如果一直没出现sunzanne ,则,一直显示到结尾。
如果在出现了一对后,又出现一个tom ,则显示到另一个suzanne为止。
? awk '$2 == "NW" || $1 ~ /south/ {print $1,$2}' datafile
记住,等于一个具体的串要加””
? awk '!($8==13) ' datafile
? awk '/southern/{print $8 - 10}' datafile
? awk ' $3 ~/Susan/ {printf "the percent is :" $6+ .2  "volume:" $8}' datafile   变成两位小数点。
? awk ' {print ($7 >4 )? "high" $7: "low " $7 }'  datafile
? awk '$3=="Chris"{$3 = "Christian";print }' datafile
如果第三个字段等于chris ,就把第三个字段赋植为”Christian “
? awk '/Derek/{$8 +=12;print }' datafile
把Derek所在的行的第8个字段加12
? awk '{$7 %= 3; print $7}' datafile
For each record, the seventh field ($7) is divided by 3, and the remainder of that division (modulus) is assigned to the seventh field and printed.
?   Awk中使用的内置变量
  
Variable Name Contents
ARGC Number of command-line argument
ARGIND Index in ARGV of the current file being processed from the command line (gawk only)
ARGV Array of command-line arguments
CONVFMT Conversion format for numbers, %.6g, by default (gawk only)
ENVIRON An array containing the values of the current environment variables passed in from the shell
ERRNO Contains a string describing a system error occurring from redirection when reading from the getline function or when using the close function (gawk only)
FIELDWIDTHS A whitespace-separated list of fieldwidths used instead of FS when splitting records of fixed fieldwidth (gawk only)
FILENAME Name of current input file
FNR Record number in current file
FS The input field separator, by default a space
IGNORECASE Turns off case sensitivity in regular expressions and string operations (gawk only)
NF Number of fields in current record
NR Number of records so far
OFMT Output format for numbers
OFS Output field separator
ORS Output record separator
RLENGTH Length of string matched by match function
RS Input record separator
RSTART Offset of string matched by match function
RT The record terminator; gawk sets it to the input text that matched the character or regex specified by RS
SUBSEP Subscript separator
? gawk  –F:  '{IGNORECASE=1};   $1 == "mary adams"{print NR, $1, $2,$NF}' employees2
? nawk 'BEGIN{FS=":"; OFS="\t"; ORS="\n\n"}{print $1,$2,$3}' file
   nawk 'BEGIN{print "MAKE YEAR"}'
awk 会在begin 完全结束后才开始读行,所以begin模块基本上都是处理些指定内置变量了,给变量赋值了,输出些提示信息了,等等的操作。  并且它都是用{}与实际读行分隔的。
? END模块
$ nawk 'END{print "The number of records is " NR }' lab4
The number of records is 12
$ nawk '/Mary/{count++}END{print "Mary was found " count " times."}' employees
Mary was found 1 times.
     $ nawk 'END{print "HEllo World!"}' lab4
END模块在所所数据行处理完之后执行,所以,它必须有处理文件,这与bgein不同。
? 重定向
输出重定向:
awk 'BEGIN{FS=":";OFS=":"} {print $1,$2>"filename"}' lab4
   重定向的文件名必须家双引号。
输入重定:
    getline 函数
nawk 'BEGIN{ "date" | getline d; print d}'
nawk '{print $1, $2 | "sort –r +1 –2 +0 –1 "}' names
:   用第二个字段做主键,第一个字段做二键,反向排序
   注意,要用双引号。
? System 函数作用
awk 'BEGIN{FS=":";OFS=":";system("ls")} {print $1,$2}' lab4
system(“ls”)
  系统命令要放在””里。
? Awk:
   Awk.sc: 
BEGIN{ FS=":"; print "\t ***CAMPAIGN 1998 CONTRIBUTIONS*** \n --------------------------------- \n name \t\t\t phone \t\t\t  jan| feb| mar| Total \n --------\n"} \
  {count1=$3+$4+$5;  bigger=($3>$4)?$3:$4;biggest=(bigger>$5)?bigger:$5;   print $1 ,"\t\t\t",$2 ,"\t\t\t",$3,$4,$5,count1} \
  {count=count+count1}\
  {ave=count/12} 
  END{print " --------------------------------------------------------------- \n \t\t\t SUMMARY  \n -------------------------------------------------------------\n "\
 " the total contrbution is : " count  "\n the average of the 12 month is : "ave   \
 " the biggest is : " biggest}
  Awk –f awk.sc lab5 
  这是lab5的答案。

? nawk '{count[$2]++}END{for(name in count)print name,count[name] }' employees
nawk '/^Tom/{name[NR]=$1};END{for( i = 1; i <= NR; i++ )print name[i]}' db

nawk  '{dup[$2]++; if (dup[$2] > 1){name[$2]++ }}  END{print "The duplicates were"  for (i in name){print i, name[i]}}' datafile4
nawk BEGIN{ split( "3/15/2004", date, "/"); print "The month is " date[1] "and the year is "date[3]"} filename
(The Output)
The month is 3 and the year is 2004.  把串3/15/2004存到date里,分隔符为/
 
BEGIN{for ( i=0; i < ARGC; i++ ){  printf("argv[%d] is %s\n", i, ARGV[i])} printf("The number of arguments, ARGC=%d\n", ARGC)}
Argc: 参数个数。
Argv : 参数数组。
? #  match 函数。
$ awk 'END{start=match("Good ole USA",/[A-S]+$/);print start,RSTART,RLENGTH}' employees
11 11 2
#  match 函数。
$ awk 'BEGIN{line="HEllo Santa Claus USA"} END{match(line,/[A-Z]+$/);print substr(line,RSTART,RLENGTH)}' employees
USA
 
 

 

nawk 'NR==2,NR==5  {print NR,$0}' datafile :  2-5行的数据输出
nawk 'BEGIN{OFMT="%.2f";print 1.2456789}'        1.25
$ awk 'BEGIN{print "\t\tWeocome to here "}{Count++}END{print Count}' datafile                 Weocome to here
9
 
 

Apr 26
2: 怎样返回grep的值到一个变量中。
   #/bin/bash
grep "abc" filename
reval=$?
echo "grep return value: $value"
exit 0
1
请问各位ld,用sed不用"-e"选项能否实现一次删除多个不连续的行?
比喻文件a的内容为 (cat -n a)
1 this is 1 line,
2 this is 2 line,
3 this is 3 line,
4 this is 4 line,
5 this is 5 line,
6 this is 6 line,
7 this is 7 line,
8 this is 8 line.
现在要删除第1行,第3行至第5行,第7行,即只剩下2,6,8行。
请问如何实现,感谢之至!!!
:em02:  :em02:
sed '1d;3,5d;7d' a
2:
  Awk中使用变量的办法:
   phonenumber=`sed -n 1p phone.txt`      # 得到每一个手机号码
   echo phonenumber:$phonenumber
 
    awk  '/'$phonenumber'/{print NR, $0  }' CMODSI_20070403050655_768.dat
11月8日
计算文件行数:
提取最后一行行号??直接当变量用??
wc -l file
tail -n 1 file
awk 'END{ print FILENAME ": " NR ":", $0}' file
输出重定向到一个文本文件里??
cmd >> file.log
老实说看不懂你要问什么.....
********************8
find . -type f -exec wc -l {} \;
find . -type f -exec wc -l {} \; | awk '{t+=$1} END {print  t}'

cat youfile|sed -e 's^,//g' |awk 'BEGIN{i=0};{i+=$1};END{print i}'
12月13日
一、 用户登陆进入系统后的系统环境变量:
$HOME 使用者自己的目录
$PATH 执行命令时所搜寻的目录
$TZ 时区
$MAILCHECK 每隔多少秒检查是否有新的信件
$PS1 在命令列时的提示号
$PS2 当命令尚未打完时,Shell 要求再输入时的提示号
$MANPATH man 指令的搜寻路径
六、测试字符串
字符串1 = 字符串2 当两个字串相等时为真
字符串1 != 字符串2 当两个字串不等时为真
-n 字符串      当字符串的长度大于0时为真
-z 字符串      当字符串的长度为0时为真
字符串       当串字符串为非空时为真
七、测试两个整数关系
数字1 -eq 数字2     两数相等为真
数字1 -ne 数字2     两数不等为真
数字1 -gt 数字2     数字1大于数字2为真
数字1 -ge 数字2     数字1大于等于数字2为真
数字1 -lt 数字2     数字1小于数字2为真
数字1 -le 数字2     数字1小于等于数字2为真
八、逻辑测试
-a         与
-o        或
!        非
1月4日
1. xargs:
    A filter for feeding arguments to a command, and also a tool for assembling the commands themselves. It breaks a data stream into small enough chunks for filters and commands to process. Consider it as a powerful replacement for backquotes. In situations where command substitution fails with a too many arguments error, substituting xargs often works. [1] Normally, xargs reads from stdin or from a pipe, but it can also be given the output of a file.
2. date的妙用
  
   Date +%j  获得从这年的开始,到目前 第多少天
   Date +%s  从1970-1-1  到现在多少天。  这个在生成临时文件是十分有用。:
        prefix=temp
          suffix=$(date +%s)  # The "+%s" option to 'date' is GNU-specific.
          filename=$prefix.$suffix
           echo $filename
           #  It's great for creating "unique" temp filenames, even better than using $$.
The -u option gives the UTC (Universal Coordinated Time).
      //格林尼制标准时间不是GMT吗?
3. zdump
Time zone dump: echoes the time in a specified time zone.
4. Cal :打出个小日历。
5. uniq
This filter removes duplicate lines from a sorted file. It is often seen in a pipe coupled with sort.
   1 cat list-1 list-2 list-3 | sort | uniq > final.list
The useful -c option prefixes each line of the input file with its number of occurrences
1月10日
1. Fold and fmt
都是把数据按照指定的宽度分割。
Fold –s –w inputfile   :按照宽度W把输入数据切割,S选项保证不会把同一个单词切割开(也就是用空格区分单词。)
Fmt –w inputfile   作用和Fold –s –w inputfile基本相同,但也有细微差别
Fold –s –w inputfile 当宽度小于两个单词而大于一个单词的时候,会将第二个单词割断。 而Fmt –w inputfile不会。
1月12日
1. 在查看文件是否存在的时候特别有用的3个命令
Which
Whereis
whatis
后面直接加命令就可以
Which  awk
2. Vdir
功能与ls –l功能相同
3. Locate slocate
Locate 文件名   获得文件的位置,带绝对路径。
1月18日
1 Tr
字符串过滤转换的命令
Either tr "A-Z" "*"
 -d   删除选项
echo "abcdef" | tr -d b-d     # aef
-s 去掉重复的命令
bash$ echo "XXXXX" | tr --squeeze-repeats 'X'
 X
-c  替换命令
bash$ echo "acfdeb123" | tr -c b-d +
      +c+d+b++++
2 Seq(jot)
These utilities emit a sequence of integers, with a user-selected increment
Seq按照用户指定的增量,生成一串整数,默认状态是增量为1
bash$ seq 5
 1
       2
 3
 4
 5
 
        bash$ seq -s : 5
       1:2:3:4:5
  在FOR循环里,很有用处。
 1) for a in `seq 80`   (for a in 1  2 3   ….80)
2) COUNT=80
 for a in `seq $COUNT`  # or   for a in $( seq $COUNT )
 3)  BEGIN=75
     END=80  
    for a in `seq $BEGIN $END`  (for a in 75 76 77 78 79 80)
4) BEGIN=45
  INTERVAL=5
   END=60  
   for a in `seq $BEGIN $INTERVAL $END`(for in 45 50 55 60 )
Mar 07
1 :
 there is a file named diff :
      It have some lines like such :
kxjp.com.               3600    IN      NS      park27.secureserver.net.
kxjp.com.               3600    IN      NS      park28.secureserver.net.
zyzygs.com.             172800  IN      NS      ns1.dns-diy.com.
zyzygs.com.             172800  IN      NS      ns2.dns-diy.com.
 it is ,there are some same lines , now ,I  hope to   evey lines are unique.. ,in other words ,I hope remove the same lines .
  I  can realize it by he way:
awk '!aa[$1]++'  diff
 
OR:
sort -k 1,1 –u  diff

2    
there are two files  :
a: 123
b:456
now ,I hope to reunion a new file :  123456
the  error method : cat a b
                 123
                  456
Now ,the true way:
     cat a b|awk 'BEGIN{RS="AAA"}{gsub(/\n/,"",$0);print}'
OR
sed "s/.*/&$(
 
Dec 29
zcat CMODVO*.Z | sed -n '/MZ *DZK/p'>/odfs01/odtemp3/1boss3ftp/RATEDEDR/MZ_DZK_200711_"$date".dat VARCHAR
zcat是处理压缩文件的cat命令
zgrep是处理压缩文件的grep命令
NOV 1 
ps -T 0
ps -T 1
 查出子孙进程
 
Sep 10  关于压缩。
 压缩:
tar
解包: tar xvf FileName.tar
打包:tar cvf FileName.tar DirName
(注:tar是打包,不是压缩!)
---------------------------------------------
.gz
解压1:gunzip FileName.gz
解压2:gzip -d FileName.gz
压缩:gzip FileName
.tar.gz
解压:tar zxvf FileName.tar.gz
压缩:tar zcvf FileName.tar.gz DirName.bz2
解压1:bzip2 -d FileName.bz2
解压2:bunzip2 FileName.bz2
压缩: bzip2 -z FileName
.tar.bz2
解压:tar jxvf FileName.tar.bz2
压缩:tar jcvf FileName.tar.bz2 DirName
---------------------------------------------
.bz
解压1:bzip2 -d FileName.bz
解压2:bunzip2 FileName.bz
压缩:未知
.tar.bz
解压:tar jxvf FileName.tar.bz
压缩:未知
---------------------------------------------
.Z
解压:uncompress FileName.Z
压缩:compress FileName
.tar.Z
解压:tar Zxvf FileName.tar.Z
压缩:tar Zcvf FileName.tar.Z DirName
---------------------------------------------
.tgz
解压:tar zxvf FileName.tgz
压缩:未知
.tar.tgz
解压:tar zxvf FileName.tar.tgz
压缩:tar zcvf FileName.tar.tgz FileName
---------------------------------------------
.zip
解压:unzip FileName.zip
压缩:zip FileName.zip DirName
---------------------------------------------
.rar
解压:rar e FileName.rar
压缩:rar a FileName.rar

rar请到: 下载!
解压后请将rar_static拷贝到/usr/bin目录(其他由$PATH环境变量指定的目录也可以):
[root@www2 tmp]# cp rar_static /usr/bin/rar
---------------------------------------------
.lha
解压:lha -e FileName.lha
压缩:lha -a FileName.lha FileName
lha请到:下载!
>解压后请将lha拷贝到/usr/bin目录(其他由$PATH环境变量指定的目录也可以):
[root@www2 tmp]# cp lha /usr/bin/
---------------------------------------------
.rpm
解包:rpm2cpio FileName.rpm | cpio -div
---------------------------------------------
.tar .tgz .tar.gz .tar.Z .tar.bz .tar.bz2 .zip .cpio .rpm .deb .slp .arj .rar .ace .lha .lzh
.lzx .lzs .arc .sda .sfx .lnx .zoo .cab .kar .cpt .pit .sit .sea
解压:sEx x FileName.*
压缩:sEx a FileName.* FileName
sEx只是调用相关程序,本身并无压缩、解压功能,请注意!
sEx请到: 下载!
解压后请将sEx拷贝到/usr/bin目录(其他由$PATH环境变量指定的目录也可以):
[root@www2 tmp]# cp sEx /usr/bin/
Shell命令
Cut
     -c   指定cut哪几个字符。
      -f  指定得到哪几列, -d指定分割符。 
       Cut –f 1,7  -d -    将得到用-分割的第一和第7列
Cut –f 1-7  -d -    将得到用-分割的第一到第7列
Date
  
date +%Y%m%d  :   
    结果 :20071115
date –u             公元2007年11月14日  星期三  02时27分11秒

 
Id
Id
uid=205(ondemand) gid=205(ondemand) groups=1(staff)     分别是用户名(id),主组(id),附加组(id)
wholenum.sh (注意什么都不做的时候的if语句模块的使用)
# Name:wholenum
    # Purpose:The expr command tests that the user enters an integer
    echo "Enter a number."
    read number
   if expr "$number" + 0 > /dev/null 2>&1
    then
       :          #  写个冒号就可以了。
    else
       echo "You did not enter an integer value." 1782
        exit 1
    fi
: $? 退出状态。
$ echo  $?
    1               Failure
Ls –d 
只显示目录,不显示文件
 

shell环境变量

如果你希望把你定义的变量让其他所有的shell程序都能使用,也就是定义新的环境变量。你只要使用export关键词就可以了
export MY_NAME=Winter
export PATH=/home/winter/bin:$PATH
上面的程序中,第一行输出MY_NAME变量,第二行是在环境变量PATH中增加一个路径/home/winter/bin 。
 

 
阅读(1814) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~