设为首页 加入收藏

TOP

Linux Shell实例精讲学习笔记(一)
2014-11-24 07:34:49 来源: 作者: 【 】 浏览:2
Tags:Linux Shell 实例 精讲 学习 笔记

第一章:shell基础
umask --查看当前用户创建文件或文件夹时的默认权限
eg:
[test@szbirdora 1]$umask
0002
[test@szbirdora 1]$ls -lh
-rw-rw-r-- test test myfile
drwxrwxr-x test test 1

上面的例子中我们看到由test默认创建的文件myfile和文件夹1的权限分别为664,775.而通过umask查到的默认权限为002.所以可以推断出umask的计算算法为:
umask file directory
0 6 7
1 5 6
2 4 5
3 3 4
4 2 3
5      1        2
6       0      1 
7       0      0


连接ln
硬连接 ln sourcefile targetfile 连接后的target文件大小和source文件一样
软连接 ln -s sourcefile targetfile 类似于windows的快捷方式

●shell script 基本结构
#!/bin/bash --------bash shell开头必须部分
# description --------注释部分(可有可无,为了阅读方便最好加以说明)
variable name=value ---------变量部分,声明变量,赋值
control segment
---------流程控制结构,如判断、循环、顺序
eg.
helloworld.sh
#! /bin/bash
# This is a helloworld shell script
printchar = "hello world"
echo $printchar

[test@szbirdora 1]$sh helloworld.sh
hello world


●shell 特性
①别名 alias eg. alias ll = “ls -l”
②管道 a |b 将a命令的输出作为b命令的输入 eg. ls |sort ls列举的项排序
③命令替换 a `b` 将b命令的输出作为a命令的输入 eg. ls `cat myfile` 列举出cat myfile的输出项
④后台运行 nohup command& 可通过jobs -l查看后台运行的脚本
⑤重定向 >,< 可以改变程序运行的输出来源和输入来源
⑥变量 可以用$varname 来调用变量
⑦特殊字符
`用来替换命令
\用来使shell无法认出其后的特殊字符,使其失去特殊含义
允许一行放多个命令
() 创建成组的命令 ??
{} 创建命令块 ??


第二章:变量和运算符

●本地变量:在用户现在的shell生命期的脚本中使用。设置变量various_name=value.可用set 来查看。用readonly可以使变量只读
●环境变量:用于当前用户下所有用户进程(不限于现在的shell)。
设置变量:export various_name=valueenv查看
readonly可以使变量只读。
●变量替换
echo ${variable name} 显示实际值到variable name
echo ${variable name:+value} 如果设置了variable name,则显示其值,否则为空
echo ${variable name: value} 如果未设置variable name,则显现用户定义错误信息value
echo ${variable name:-value} 如果未设置,则显示其值
echo ${variable name:=value} 如果未设置,则设置其值,并显示
●清除变量
unset variable name
●位置变量
位置变量表示$0,$1,$2...$9
$0 ----脚本名字
$1 ----根据参数位置表示参数1
eg.
#! /bin/bash
#parm.sh
echo "This is script name : $0"
echo "This is parameter 1: $1"
echo "This is parameter 2: $2"
[test@szbirdora 1]$sh parm.sh a b
This is script name : parm.sh
This is parameter 1: a
This is parameter 2: b

●向系统中传递位置变量
#!/bin/bash
#parm.sh
find /u01/test/1 -name $1 -print
[test@szbirdora 1]$ sh parm.sh myfile
/u01/test/1/myfile

●标准变量 bash默认建立了一些标准环境变量,可在/etc/profile中定义
EXINIT
HOME
IFS
LOGNAME --当前 录用户名
MAIL
MAILPATH
PATH
TERM --终端信息
TZ --时区
PS1
--登录提示,如[test@szbirdora 1]$
[test@szbirdora 1]$ echo $PS1
[\u@\h \W]\$ --\u -user --\h -host --\W -documen

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇alarm信号定时运行程序 Linux 下一篇Linux中的sleep和alarm在延时作用..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis 分布式锁全解 (2025-12-25 17:19:51)
·SpringBoot 整合 Red (2025-12-25 17:19:48)
·MongoDB 索引 - 菜鸟 (2025-12-25 17:19:45)
·What Is Linux (2025-12-25 16:57:17)
·Linux小白必备:超全 (2025-12-25 16:57:14)