Shell 基础

1.  文件操作

     a.  创建,删除

mkdir  创建文件夹;      
rmdir  删除文件夹;      
touch  创建文件
rm     删除文件;

    b.  文件读取

          1. 反向读取文件   command: tac

tac test.txt | grep exception

            2. 循环读取文件

cat test.txt | while read line
do
        echo $line
 done 

           3. 用 set 获取 某行的数据

set a = ` sed -n  '1p' test.txt `       (或: set  ` sed -n '1p' text.txt  `)
echo $1 

      c.  重命名文件

mv a b  //  不保留原来的文件

cp a b   //  保留原来的文件

      d. 判断文件是否存在

          if [ -f filename ] then ....;

      e. 权限操作               

$ ls -l
total 4332
-rwxr-xr-x

   1   root    root   4332  Oct   14   04:44   test.txt

1. 文见权限概述 

文件的三种访问方式, 读、写、执行(如果该文件是一个 shell 程序)

针对相应的用户,文件的权限包括

1) 文件属主,创建该文件的用户。

2) 同组用户,拥有该文件的用户组中的任何用户。

3) 其他用户,即不属于拥有该文件的用户组的某一用户。     

2. 解析 ( 例: -rwxr-xr-x )

total 4332: 该目录中所有文件所占的磁盘空间 

-rwxr-xr-x  如果除去最前面的横杠 (表示普通文件),这里一共是9个字符,他们分别对应9个权限位

rwx            文件属主权限 这是前面三位

r-x             同组用户权限 这是中间三位

r-x             其他用户权限 这是最后三位

1               该文件硬链接的数目。

root          文件的属主。

root          文件的属主root所在的缺省组(也叫做root )。

3578        用字节来表示的文件长度,记住,不是K字节!

Oct 14 04:44: 文件的更新时间。

test.txt     文件名称

3. 权限解析

文件属主权限          组用户权限          其他用户权限

rwx                            r-x                           r-x

r   读权限; w  写/更改权限; x   执行该脚本或程序的权限

4. 修改文件权限

chmod [who] operator [permission] filename

who            -> u 文件属主权限; g 同组用户权限;  o 其他用户权限; a 所有用户(文件属主、同组用户及其他用户).

operator     -> + 增加权限        - 减少权限         = 设定权限

permission  -> read;  w  write; write; s 文件属主和组set-ID; t 粘性位*; l lock

 如果在一个目录上出现“t”位,这就意味着该目录中的文件只有其属主才可以删除

Example on how to change the permission:

$ touch myfile
$ ls -l myfile
-rw-r--r--  1    dave    admin     0 Feb 19  22:05  myfile
$ chmod  u+x  myfile
$ ls -l myfile
-rwxr--r--  1    dave    admin     0 Feb 19  22:05  myfile
$ chmod o-r    myfile
$ ls -l myfile
-rwxr-----  1    dave    admin     0 Feb 19  22:05  myfile

 

2.  字符操作

      a. 特殊字符处理

          1. 打印特殊字符.  转意字符

$ echo "\""
   "                
$ echo "\'"
   '

           2. 单引号在AWK中打印方式不同,需要 "'\''" 才能打印成功 - 在本地Cygwin on windows 测试通过

$ echo "a" | awk '{ print $1 }'
a
$ echo "a" | awk '{print "'"$1"'"}'
                   # Nothing print out here
$ echo "a" | awk '{print $1"'\''"}'
a'

      b. 字符长度|文件长度

           如果有一份名为 test 的文本文件, 且内容如下:

ab
abc

          获取这个文件的长度

$ cat test | awk '{len += length($0)}END{ print len}'
4

         同时也可以用wc 处理.

$ wc --help
Usage: wc [OPTION]... [FILE]...
  or:  wc [OPTION]... --files0-from=F
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified.  With no FILE, or when FILE is -,
read standard input.
  -c, --bytes            print the byte counts
  -m, --chars            print the character counts
  -l, --lines            print the newline counts
      --files0-from=F    read input from the files specified by
                           NUL-terminated names in file F
  -L, --max-line-length  print the length of the longest line
  -w, --words            print the word counts
      --help     display this help and exit
      --version  output version information and exit

3. Time

a. 时间差

begintime=`date +%s`

endtime=`date +%s`

duration=`expr $endtime - $begintime`

(+%s 或者 +%S均可)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值