Shell脚本学习之一:文件属性与权限管理

 

1.1 shell概念

定义:shell无非就是kernel外层的一个命令解析器,把结果返回给用户。shell脚本=Dos批处理文件(.bat)

分类:ash, bash, ksh, csh, tcsh

系统支持的shell种类:cat /etc/shells

当前运行的shell类型:echo $SHELL     (注意这里是大写)

 

 

1.2 文件权限和安全

linux作为多任务系统,需顾及每个人、群组的隐私安全,故任何一个文件都有User,GroupOthers三个权限。

1.2.1 文件属性

相关命令:

ls -l / ls -lh : 列出当前目录下所有大小及属性。

[root@Gwan Desktop]# ls -lh

total 16K

-rwx------  1 root root  248 May 12 02:07 firefox3.desktop

-rwx------  1 root root  189 May 12 02:14 firefox.desktop

lrwxrwxrwx  1 root root   26 May 12 02:06 link to firefox -> /usr/local/firefox/firefox

-rwx------  1 root root  302 May 12 14:03 opera.desktop

drwxr-xr-x  2 root root 4.0K Mar  9  2010 untitled folder

文件属性  文件数 拥有者 所有者群组 大小 创建时间 文件名


【备注】

1.       上面untitiled folder的文件夹/目录(d),如果要群组或其他人查看(进来),必须要有Xexecutable)权限,所以我们为什么能经常看到744, 对于文件就无所谓了。

2.       ls更详细的用法: man ls/ info ls

 

1.2.2 改变文件权限

   chgrp  [-R]  : change group, 要改变成为群组的名称必须在/etc/group下能找到, 否则报错!

   chown  [-R]  : change owner

   chmod  [-R]  : change mode, 改变文件属性 r– 4, w-2, x-1

   umask : 指定用户在创建文件或目录时的属性缺省值。

相关操作:

[root@Gwan Desktop]# mkdir wade

[root@Gwan Desktop]# chgrp Tao wade

[root@Gwan Desktop]# ls -lh

total 20K

-rwx------  1 root root  248 May 12 02:07 firefox3.desktop

-rwx------  1 root root  189 May 12 02:14 firefox.desktop

lrwxrwxrwx  1 root root   26 May 12 02:06 link to firefox -> /usr/local/firefox/firefox

-rwx------  1 root root  302 May 12 14:03 opera.desktop

drwxr-xr-x  2 root root 4.0K Mar  9  2010 untitled folder

drwxr-xr-x  2 root Tao  4.0K Oct  5 08:41 wade

[root@Gwan Desktop]# chgrp Gwan wade

chgrp: invalid group name `Gwan'

 

同时改变文件/目录的所有者和属组

[root@Gwan ~]# ls -l who.out

-rw-r--r--  1 root root 49 Aug 13 10:47 who.out

[root@Gwan ~]# chown oracle.dba who.out

[root@Gwan ~]# ls -l who.out

-rw-r--r--  1 oracle dba 49 Aug 13 10:47 who.out

 

看新建的文件或目录的缺省值

umask

[root@Gwan ~]# umask                     # 查看umask初始值

0022

[root@Gwan ~]# touch filename              # 创建文件filename

[root@Gwan ~]# ls -l filename

-rw-r--r--  1 root root 0 Oct  6 15:48 filename      #文件默认的是644

 

[root@Gwan ~]# mkdir directory

[root@Gwan ~]# ls -l directory

total 0

[root@Gwan ~]# ls -lda directory

drwxr-xr-x  2 root root 4096 Oct  6 15:51 directory    #目录默认是755

规律是什么?

文件预设没有X权限,为666, 而目录有X可以进入该目录,所以默认为777

umask指的是该默认值需要剪掉的权限, 755对应022666-022=644 777-022=722对吗?

改变umask的值

[root@Gwan ~]# umask 003

[root@Gwan ~]# touch test1

[root@Gwan ~]# mkdir test2

[root@Gwan ~]# ls -lda test1 test2

-rw-rw-r--  1 root root    0 Oct  6 16:04 test1    #缺省是664

drwxrwxr--  2 root root 4096 Oct  6 16:04 test2   #缺省是774

666 – 003 = 663 -rw-rw--wx, 777 – 003 = 774 drwxrwxr--  

貌似这样才对!误区:数字相加减,用二进制不要用十进制

应该:  [rw-rw-rw-] – [-------wx] = [rw-rw-r-] 

      Umask可在/etc/bashrc下更改,rootumask022, 一般用户是002, 因为rootW权。

快速查找umask, vi umask然后输入/umask, 或者直接cat /etc/bashrc | grep umask

对于特定用户要修改其umask可以在$HOME/.profile  $HOME/.bash_profile中修改

 

 

1.2.2 符号链接

硬链接: 磁盘上一个文件有多个指向,同一文件系统,文件内容和大小都一样。不能指向目录文件。

ln 源文件 目标文件

[root@Gwan ~]# ln term.txt term-hl

[root@Gwan ~]# ls -l | grep term

-rw-r--r--  2 root   root        83 Aug 19 00:34 term-hl

-rw-r--r--  2 root   root        83 Aug 19 00:34 term.txt

 

软连接: 也成符号链接,类似于瘟到死下面的快捷方式

ln [-s]  源文件 目标文件

[root@Gwan ~]# ls -l |grep term

-rw-r--r--  2 root   root        83 Aug 19 00:34 term-hl

lrwxrwxrwx  1 root   root         8 Oct  6 16:53 term-sl -> term.txt  

-rw-r--r--  2 root   root        83 Aug 19 00:34 term.txt

 

 

1.3 Shell脚本

1.3.1 几本元素

#! /bin/bash  

#   注释

变量

流程控制结构

 

写第一个脚本

STEP 1: locale查看语言环境变量(local -a 显示系统支持的所有语系)

[root@Gwan ~]# locale

LANG=en_US.UTF-8

LC_CTYPE="en_US.UTF-8"

LC_NUMERIC="en_US.UTF-8"

LC_TIME="en_US.UTF-8"

LC_COLLATE="en_US.UTF-8"

LC_MONETARY="en_US.UTF-8"

LC_MESSAGES="en_US.UTF-8"

LC_PAPER="en_US.UTF-8"

LC_NAME="en_US.UTF-8"

LC_ADDRESS="en_US.UTF-8"

LC_TELEPHONE="en_US.UTF-8"

LC_MEASUREMENT="en_US.UTF-8"

LC_IDENTIFICATION="en_US.UTF-8"

LC_ALL=

改变系统支持的语系,可修改cat /etc/sysconfig/i18n

[root@Gwan ~]# cat /etc/sysconfig/i18n

LANG="en_US.UTF-8"

SUPPORTED="en_US.UTF-8:en_US:en"

SYSFONT="latarcyrheb-sun16"

 

STEP 2: 设置简体中文

[root@Gwan ~]# LANG=zh_CN.UTF-8

[root@Gwan ~]# locale

LANG=zh_CN.UTF-8

LC_CTYPE="zh_CN.UTF-8"

LC_NUMERIC="zh_CN.UTF-8"

LC_TIME="zh_CN.UTF-8"

LC_COLLATE="zh_CN.UTF-8"

LC_MONETARY="zh_CN.UTF-8"

LC_MESSAGES="zh_CN.UTF-8"

LC_PAPER="zh_CN.UTF-8"

LC_NAME="zh_CN.UTF-8"

LC_ADDRESS="zh_CN.UTF-8"

LC_TELEPHONE="zh_CN.UTF-8"

LC_MEASUREMENT="zh_CN.UTF-8"

LC_IDENTIFICATION="zh_CN.UTF-8"

LC_ALL=

问:要还原设置怎么办呢?直接输入LANG=en_US.UTF-8

问:我这么设置了,怎么还是显示乱码?看工具选项的外观设置(appearance):宋体+UTF-8

 

STEP 3: 编写脚本

[root@Gwan ~]# cat helloworld.sh

#!/bin/bash

#这是一个打印"hello world"的脚本

printchar="hello world !"  #定义一个变量

echo $printchar

 

STEP 4: 给这个文件可执行权限

[root@Gwan ~]# chmod u+x helloworld.sh

[root@Gwan ~]# ll

总用量 664936

-rw-r--r--  1 root root 668734007  3 10 20:43 10201_database_linux32.zip

-rw-r--r--  1 root root      1150  3  8 04:33 anaconda-ks.cfg

drwxr-xr-x  3 root root      4096  5 12 14:03 Desktop

-rw-r--r--  1 root root  10197042  5 12 01:01 firefox-3.6.3.tar.bz2

-rw-r--r--  1 root root   1167196  3  9 20:34 gnome-libs-devel-1.4.1.2.90-44.1.i386.rpm

drwxr-xr-x  2 root root      4096  3  9 21:03 GwanRPMS

-rwxr--r--  1 root root       117  8  2 01:02 helloworld.sh

[root@Gwan ~]# ./helloworld.sh

hello world !

 

 

1.4 Shell特性

别名, 管道, 重定向, 后台处理, 模式匹配, 命令替换.

[root@Gwan ~]# alias     在哪个文件中储存呢? $HOME/.bashrc

alias cp='cp -i'

alias l.='ls -d .* --color=tty'

alias ll='ls -l --color=tty'    #缺省模式

alias lm='ls -al | more'

alias ls='ls --color=tty'

alias vi='vim'

alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

[root@Gwan ~]# alias ll='ls -alh'

[root@Gwan ~]# ll

总用量 650M

drwxr-x---  25 root root 4.0K  8  2 01:02 .

drwxr-xr-x  24 root root 4.0K  8  1 23:36 ..

-rw-r--r--   1 root root 638M  3 10 20:43 10201_database_linux32.zip

-rw-r--r--   1 root root 1.2K  3  8 04:33 anaconda-ks.cfg

-rw-------   1 root root 3.9K  8  2 00:41 .bash_history

....

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

转载于:http://blog.itpub.net/24463783/viewspace-675509/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值