umask :遮罩码
- 文件默认不能具有执行权限,如果算的结果中有执行权限,则其将权限加1
在预设的情况中, root的 umask 会拿掉比较多的属性,roo的umask 默认是 022 , 这是基于安全的考虑啦~至于一般身份使用者,通常他们的 umask 为 002 ,亦保留同群组的写入权力! 其实,关亍预设 umask 的设定可以参考 /etc/bashrc 这个档案的内容。
root@VM-0-3-ubuntu:~# umask
0022
$ umask
0002
- 若使用者建立为『档案』则预设『没有可执行( x )权限』,亦叧有 rw 这两个项目,也就是最大为 666 分,预设权限如下:-rw-rw-rw-
- 若用户建立为『目录』,则由于 x 不是否可以进入此目录有关,因此默设为所有权限均开放,亦即为 777 分,预设权限如下:drwxrwxrwx
umask:023
文件:666-023=643
目录:777-023=754
$ umask 023
$ umask
0023
$ touch hellosa
$ ls -l hellosa
-rw-r--r-- 1 ywu ywu 0 Oct 19 22:46 hellosa
$ mkdir testdir
$ ls -ld testdir/
drwxr-xr-- 2 ywu ywu 4096 Oct 19 22:47 testdir/
$ # -rw-r--r-- 644而不是643?文件默认不能具有执行权限,如果算的结果中有执行权限,则其将权限加1
$ # drwxr-xr-- 754
站在用户登录的角度来说,SHELL的类型
-
登陆式shell:
1.正常通过某终端登录
2.su - USERNAME
3.su -1 USERNAME -
非登陆式shell:
1.su USERNAME
2.图形终端下打开的命令窗口
3.自动执行的shell脚本 -
bash的配置文件:
全局配置:/etc/profile, /etc/profile.d/*.sh, /etc/bashrc
个人配置:~/ .bash_profile, ~/ .bashrc -
proflie类的文件:
设定环境变量
运行命令或脚本 -
bashrc类的文件:
设定本地变量
定义命令别名 -
登陆式shell如何读取配置文件
/etc/profile --> /etc/profile.d/*.sh --> ~/ .bash_profile --> ~/ .bashrc --> /etc/bashrc -
非登陆式shell如何配置文件:
~/ .bashrc --> /etc/bashrc --> /etc/profile.d/*.sh
shell编程:
-
编译器,解释器
-
编程语言:机器语言、汇编语言、高级语言
静态语言:编译型语言
强类型(变量)
事先转换成可执行格式
+ C、C++、JAVA、C#
动态语言:解释型语言,on the fly
弱类型
边解释边执行
PHP、SHELL、python、perl
编程模型
面向过程:Shell,C
面向对象:JAVA,Python,perl,C++
变量:内存空间,命令
内存:编址的存储单元
编程能力
脚本编程
bash变量类型:
1.环境变量
2.本地变量(局部变量)
3.位置变量
4.特殊变量
本地变量:
- VARNAME=VALUE:作用域为整个bash进程
局部变量:作用域为当前shell进程及其子进程
1.export VARNAE=VALUE
2.VARNAME=VALUE
3.export VANAME
环境变量:
- 作用域为当前shell进程及其子进程
位置变量:$1,$2,...
;shift
:
# 举例说明:$1\$2代表的意义:
./filetest.sh /etc/fastab /etc/inittab
$1:/etc/fastab
$2: /etc/inittab
# 练习:写一个脚本
能接受一个参数(文件路径)
判定:此参数如果是一个存在的文件,就显示“ok”;否则显示不存在:
ubuntu@VM-0-3-ubuntu:~$ nano filetest.sh
ubuntu@VM-0-3-ubuntu:~$ cat filetest.sh
# !/bin/bash
if [ -e $1 ];then
echo "OK"
else
echo "No such file."
fi
ubuntu@VM-0-3-ubuntu:~$ chmod +x filetest.sh
ubuntu@VM-0-3-ubuntu:~$ ./filetest.sh
OK
ubuntu@VM-0-3-ubuntu:~$ ./filetest.sh /etc/fstab
OK
ubuntu@VM-0-3-ubuntu:~$ ./filetest.sh /etc/fstabb
No such file.
# 查看shift的意义:
ubuntu@VM-0-3-ubuntu:~$ nano shift.sh
ubuntu@VM-0-3-ubuntu:~$ cat shift.sh
echo $1
shift 2
echo $1
shift 1
echo $1
ubuntu@VM-0-3-ubuntu:~$ ./shift.sh 1 2 3 4 5 6 7
1
3
4
特殊变量:
$?
:上一个命令的执行状态返回值
$#
:参数的个数
$*
:参数列表
ubuntu@VM-0-3-ubuntu:~$ nano filetest.sh
ubuntu@VM-0-3-ubuntu:~$ cat filetest.sh
# !/bin/bash
echo $#
echo $*
if [ -e $1 ];then
echo "OK"
else
echo "No such file."
fi
ubuntu@VM-0-3-ubuntu:~$ ./filetest.sh /etc/fstab /etc
2
/etc/fstab /etc
OK
程序执行,可能有两类返回值:
程序执行结果
程序状态返回码(0-255):0代表正确,1-255错误执行(1,2,127系统预留)
输出重定向:
>
>>
2>
2>>
$>
+撤销变量:
unset VARNAME
- 查看变量:
set
- 查看当前shell中的环境变量:
printenv
env
export
- 脚本:命令的堆砌,按实际需要,结合命令流程控制机制实现的源程序
shebang:魔数
#!/bin/bash
脚本的第一行必须这样写。
而后以#开始的都被当做解释行,不运行
root@VM-0-3-ubuntu:~# ls /var
backups cache crash lib local lock log mail opt run snap spool tmp
root@VM-0-3-ubuntu:~# nano first.sh
root@VM-0-3-ubuntu:~# nano first.sh
root@VM-0-3-ubuntu:~# ls
first.sh
root@VM-0-3-ubuntu:~# ls -l first.sh
-rw-r--r-- 1 root root 21 Oct 24 21:40 first.sh
root@VM-0-3-ubuntu:~# chmod +x first.sh
root@VM-0-3-ubuntu:~# ls -l first.sh
-rwxr-xr-x 1 root root 21 Oct 24 21:40 first.sh
root@VM-0-3-ubuntu:~# first.sh
first.sh: command not found
root@VM-0-3-ubuntu:~# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
root@VM-0-3-ubuntu:~# pwd
/root
root@VM-0-3-ubuntu:~# ./first.sh
backups cache crash lib local lock log mail opt run snap spool tmp
root@VM-0-3-ubuntu:~# chmod -x first.sh
root@VM-0-3-ubuntu:~# ls -l first.sh
-rw-r--r-- 1 root root 21 Oct 24 21:40 first.sh
root@VM-0-3-ubuntu:~# bash first.sh
backups cache crash lib local lock log mail opt run snap spool tmp
网友评论