文章目录
快速入门
修改密码
保存密码的地方
/etc/passwd
他的每个条目有7个域,分别是名字:密码:用户id:组id:用户信息:主目录:shell 例如:ynguo:x:509:510::/home/ynguo:/bin/bash
/etc/shadow
/etc/shadow这个影子文件,密码放在这个文件里面,并且是只有root可读的。在利用了shadow文件的情况下,密码用一个x表示,普通用户看不到任何密码信息。如果你仔细的看看这个文件,会发现一些奇怪的用户名,她们是系统的缺省账号,缺省账号是攻击者入侵的常用入口,因此一定要熟悉缺省账号,特别要注意密码域是否为空。
修改密码指令
passwd
重启
重启
reboot
shutdown -r
关机
shutdown -h
选择shell
没啥用,有这些shell可以用/bin/bash, csh, sh, ksh
chsh
Bash是Linux下的标准shell。当Linux系统创建一个用户时,如果不特别指定,采用的默认shell就是Bash。Bash是在1979年由Setphen Bourne开发完成的。它具有功能简单、运行速度快的特点。Bash具有DOS系统下doskey的功能,用户以前输入过的命令可以通过上下箭头进行快速查阅。
Csh是于20世纪70年代发展起来的,它是由加州大学伯克利分校的Bill Joy设计,提供了部分Bash不具有的功能。Csh是BSD版本中UNIX系统中的标准Shell。在进行shell程序设计时,Csh提供类似于C的语法,这也是被称为Csh的原因,其缺点是运行速度太慢。
Ksh是Korn shell 产生于20世界80年代中期,它是Bash的一个超集。Ksh集成了bash与csh的优点,同时在运行速度上有所提升。
自启动文件
每次登陆执行
$HOME/.bash_profile
fork a shell执行
$HOME/.bash_rc
设置自己的启动文件吧
echo "Hello from rc"
PS1="\[\e[32;1m\][\[\e[32;1m\]\u \[\e[36;1m\]@ \[\e[33;1m\]\h \[\e[35;40m\]\W\[\e[37;1m\]] \[\e[0;1m\]\\$\[\e[0m\] "
alias copy='cp'
alias delete='rm -i'
alias rename='mv'
alias du='du -h'
alias cls='clear'
alias yasuo='bzip2 -k -v'
alias jieya='bzip2 -d'
alias rm='rm -rfi'
alias cat='cat -n'
alias cp='cp -ai'
PATH=$PATH:.
PATH=$PATH:~
查看环境变量
查看一个环境变量
添加的dir
echo $PATH
home地址
echo $HOME
命令行
echo $PS1
语言
echo $LANG
查看所有
env
set
设置环境变量
PS1=''
PATH=$PATH:.
设置别名
alias ls='ls --color=auto'
查看设置的别名
alias
查看当前路径
pwd
快捷键
ctrl a
移动到开头
ctrl e
移动到结束
ctrl u
光标前面的全部删除
ctrl k
光标后面的全部删除
文件管理系统
Unix 设备
SCSI/SATA Hard Disk /dev/sd[a-p]
USB Disk /dev/sd[a-p]
Printer /dev/lp[0-2]
当硬盘接入时,第一块会被系统识别成/dev/sda,此时接入第二块是/dev/sdb,若再接入U盘/dev/sdc
/dev是包含所有设备文件的目录
/sda是设备的id
通常见到的是/sda5,这里的5呢就是windows系统中的分区,Linux中用的是12345…windows是CDEFG…
df
df (disk free) ,用于显示目前在 Linux 系统上的文件系统磁盘使用情况统计。
还有一个容易混淆是du (英文全拼:disk usage)命令用于显示目录或文件的大小。
输入df,显示当前系统接入的硬件所对应的设备文件名,块大小,已使用情况,空闲情况,和挂载位置,这里服务器用的是阵列所以和常见的不太一样…
常见的/dev/sda1会被挂载到/
如果有个/dev/sda2挂载/boot,我输入cd /boot/grub,系统会如何识别是在sda2的扇区上呢?系统首先 /boot/grub是否是挂载点,会发现不是,紧接着判断/boot,发现/dev/sda2被挂载到了/boot,那么就确定了是在这块扇区上
传统的挂载点有
/
swap
/boot
如何配置新加入的硬盘分区
Make the File System work
- 磁盘分区 fdisk,linux分区工具,开始进去分区
fdisk /dev/sdb
fdisk:【-l】列出该硬盘设备的分区表。
该命令主要工作是修改分区表,定义某一分区是从 n1 磁柱到 n2 磁柱,并不实际切割硬盘,若分区错误,只需在格式化之前改回。 - 格式化 format
- 挂载 mount,设置挂载点
mount /dev/sdb1 /mnt
- 格式化,分区上建立文件系统, -t ext4指定档案系统的型式
mkfs -t ext3 /dev/sdb1
mkfs 本身并不执行建立文件系统的工作,而是去调用相关的程序来执行。
例如,在"-t" 参数中指定 ext2,将 sda7 分区格式化为 ext2 格式
mkfs -t ext2 /dev/sda7
文件类型
普通文件
目录
软连接
设备文件 - 块特殊文件和字符特殊文件
Named pipe
Socket
linux目录结构:FHS
Filesystem Hierarchy Standard
关于文件常见的命令
显示目前在 Linux 系统上的文件系统磁盘使用情况统计
df
显示目录或文件的大小,-h以K,M,G为单位显示
du -h
pwd
cd
mkdir
-p 确保目录名称存在,不存在的就建一个,常用在递归创建
mkdir -p
rmdir
递归删除目录
rmdir -p
ls -l
ugo权限 所有者 group 字节大小 时间 文件地址
rm
cp
mv
辨识文件类型
file
修改文件或者目录的时间属性,包括存取时间和更改时间。若文件不存在,系统会建立一个新的文件。
touch -c -t 201604061223.33 file
-rw-r–r-- 1 s18 stu 0 2016-04-06 12:23 file
- c 假如目的档案不存在,不会建立新的档案。与 --no-create 的效果一样。
- t 设定档案的时间记录,格式与 date 指令相同。
touch
检查文件系统
fsck
文件表示和存储
unix中文件索引用inode
inode table内容:
File’s location on disk采用1级、2级、3级索引表示
标准文件和文件描述符
当程序要打开某个文件,首先查找File Descriptor,依次向右查找Systemwide file table, Systemwide inode table,inode table for 特定的文件,找到物理地址
File Descriptor前3个放的是输入输出设备,后面是文件的索引,第三个是错误输出
逻辑盘卷管理
有多个物理磁盘,可以通过逻辑盘卷管理将这些物理磁盘都归为一个逻辑盘卷
作用
当逻辑盘卷的分卷满时,可以通过添加物理磁盘归入逻辑盘卷进行扩充
使用
- 使用fdisk,将要归入一个逻辑盘卷的磁盘的磁盘文件System ID都改成一致
- 使用pvereate,pvscan建立与观察physical volume(物理卷)
- 使用vgereate,vgdisplay以物理卷建立Volume Group
- 使用lvereate,lvdisplay从VG分割出logical volume
- 使用mkfs,mount格式化与挂载到系统中
文件安全
访问权限
用户类型:User(owner),group,other
一个user可以在多个group
访问权限:Read,write,execute
目录的访问权限:
-Read:列出文件
-Write:创建和删除文件和目录
-Execute:目录搜索
简单来说:x对目录来说是许你进入目录也就是cd 目录 而r相当于就是ls列出目录里面的文件和目录了,会影响目录里面的文件rwx
↓看这个
没了x不能进入目录,但有r还是能列出里面的文件的,但是不能对里面的文件进行操作
ls -ld \
查看目录权限
ls -l
查看文件权限
文件所有者信息
id
user
group
chown
更改文件所有者
chgrp
更改文件的群组
更改文件权限
有两种方式
- chmod [ugo] [+-] [rwx]
- chmod 777
chmod ugo+x file
chmod 777 file
r 4
w 2
x 1
默认文件权限
用777或者666减去掩码值
777 - mask
666 - mask
umask xxx
通过umask设置掩码值
比如umask 077
那么文件默认权限就是700
特殊访问位
- SUID 当可执行文件设置了SUID,其他人使用命令时切换成owner权限
chmod 4xxx file
chmod u+s file
- SGID 设置后,其他人访问文件时切换成group权限
chmod 2xxx file
chmod g+s file
- sticky bit 可以创建文件,但只能删除自己的文件,目录增加黏着位,粘着位只对目录有效
chmod 1xxx dir
chmod +t dir
设置文件权限:chmod xxxx file,有四个数字,第一个x对应特殊访问位
文件处理
常用命令 cat less/more od head tail wc
-
cat
-
cat -n
显示行数 -
less/more (-N)
查看文件(显示行数) -
od file
以不同进制查看文件 -
od -c file
查看不可见字符,一般用来看\t \r \n -
head -n 3 file
显示文件头3行 -
tail -3 file
显示文件的尾部3行 -
tail -f file
文件有新增内容,实时进行显示 -
wc file
获得文件的行数为、单词数、字节数 -
wc -c file
文件byte大小 -
wc -l
行数 -
wc -w
单词数
查看文件区别 diff
- diff
diff file1file2
2a3 增加,4c5 修改, 7,8d7 删除 - uniq file
连续出现行不显示 - uniq -c file
显示重复行数
压缩文件 gzip/bzip2
压缩文件有两种格式,gzip和bzip2
- gzip file
压缩文件,但不保留原来的文件
-d 解压 - bzip2 file
压缩文件,但不保留原来的文件
-d 解压 - gzip,bzip2和xz下,我们还可以使用一个“ -c ”的参数,就可以将数据重定向,从而不去丢失原文件。
gzip -c file1 > file1.gz - 在bzip2和xz下有一个“ -k “的参数,可以直接保留原文件,也是最方便的一种,但如果你想用最节省时间的gzip,那就没有这个方便的选项了。
bzip2 -k -v file1
补充tar
- tar压缩
tar –zcvf a.tar.gz a - tar解压
tar -zxvf a.tar.gz
文件查找 find
-
find /tmp -name ‘file_name.*’
以名字查找,要加单引号 -
find /tmp -size ([+-])1[KMG]
查找大小为(大于或小于)1(kb/M/gb)的文件 -
find /tmp (-size ([+-])1[KMG] -a -name ‘file_name.*’ )
-a并且 -o或者 用斜杠括号括起来 -
find /tmp -size +1M -exec ls -l {} \;
找到的文件都执行ls -l命令,{}代表占位符 -
find /tmp -size -1k -ok rm {} \;
删除小于1k的文件,再删除前进行确认 -
find / -type f -size 0 -exec ls -l {} \;
查找系统中所有文件长度为0的普通文件,并列出它们的完整路径
查找命令位置
- which command
- whereis file
在$PATH目录下查找文件
对文件内容排序 sort
- sort file
每行开头进行比较,字典排序 - sort -k 3 file
以第三个字段进行排序 - sort -k4 -n(-nk4) file -n 依照数值的大小排序
以数字大小进行排序 从小到大,-r 逆序 - sort -t: -k4 /etc/passwd
-t 指定字段分隔符
按列查看 cut
cut -d' ' -f1 cut.txt
-d,–delimiter=分界符 ,使用指定分界符代替制表符作为区域分界
cut -f -2 student_record
-f, --fields=列表, 只选中指定的这些域;并打印所有不包含分界符的行,除非-s 选项被指定
cut -c3,4,8-11 stu.txt
-c, --characters=列表,只选中指定的这些字符,选第几个字符,cut -c只能单独使用
20181003054 -> 183054
拼接文件 paste
paste cut.txt cut1.txt
1 one 1 yi
2 two 2 er
3 three 3 san
查找文件内容 grep
grep
用法: grep [选项]… PATTERN [FILE]…
在每个 FILE 或是标准输入中查找 PATTERN。
默认的 PATTERN 是一个基本正则表达式(缩写为 BRE)。
例如: grep -i ‘hello world’ menu.h
-i 忽略大小写
-n 显示行号
-v 显示未匹配
-c 计数
-e 使用拓展正则
-w 搜索pattern
例子
1.grep ‘San’ /tmp/databook
2.grep -E 'J[a-z]+ ’ /tmp/databook
3.grep ‘700$’ /tmp/databook
4.grep -v -e ‘834’ /tmp/databook
5.grep -E ‘:[0-9]+/12/[0-9]’ /tmp/databook
6.grep -E ‘408-[0-9]+-[0-9]+’ /tmp/databook
7.grep -E ‘[A-Z][a-z]{4}, [A-Z]’ /tmp/databook
8.grep -E ‘[^,] \<(k|K)’ /tmp/databook
9.grep -n ‘[0-9]{6}’ /tmp/databook
10.grep -i ‘lincoln’ /tmp/databook
一次性处理显示文件 sed
替换 s
- sed ‘s/要被取代的字串/新的字串/g’
sed 's/^ *[0-9]*//g' hello2.c
-i 保存修改后文件, g的意思是全局替换
sed -i 's/^ *[0-9]*//g' hello2.c
sed ‘/reg/s/要被取代的字串/新的字串/g’
sed '/^s18/s/bash/nolog/g /etc/password'
替换后打印
sed -n '/^Popeye/s/:[0-9]*\/[0-9][0-9]/:11\/14\/46/p' /tmp/databook
删除 d
- sed ‘行号,行号/reg/d’ 文件名
删除2到5行
sed '2,5d' hello2.c
删除所有的空行
sed '/^[ \t]*$/d' hello2.c
删除注释行
sed '/\/\//d' hello.c
添加 a
打印 p
要用-n才能指定行,不然都打印出来了
例子
替换例子:
sed 's/Jon/Joanthan/g' /tmp/databook
删除例子:
sed '1,3d' /tmp/databook
sed '/Lane/d' /tmp/databook
sed '/^ *$/d' /tmp/databook
打印例子:
sed -n '5,10p' /tmp/databook
sed -n '/:1[12]\\//p' /tmp/databook
替换字符串后打印:
sed -n 's/Jose/JOSE HAS RETIRED/p' /tmp/databook
sed -n '/^Popeye/s/:[0-9]*\/[0-9][0-9]/:11\/14\/46/p' /tmp/databook
grep的改进版 awk
可以当grep用,还支持字段的概念
awk '/\<CS/' /tmp/student_record
打印第一和第二个字段
awk '{print $1,$2}' /tmp/student_record
表达式:”awk ‘/reg/{operation}/’
awk '/\<CS/{print $1,$2}' /tmp/student_record
-F 指定分隔符 -F‘reg’ -F:
awk -F'[: ]' '/^s18/{print $1,$3}' /etc/passwd
查找字段小于,支持算数运算 $4<
awk '$4<2.5' /tmp/student_record
支持比较表达式 && ||
awk '$3=="CS" && $4<3.5' /tmp/student_record
~ 字段匹配值
awk '$2~/^[Kk]/' /tmp/databook
例子
选择列 打印
awk -F: '{print $2}' /tmp/donors
awk -F: '/Dan/{print $2}' /tmp/donors
awk -F: '/Susan/{print $1,$2}' /tmp/donors
字段匹配值 打印
awk '$2~/^D[a-z]*/{print}' /tmp/donors
awk '$1~/\^[C-E][a-z]*/ {print}' /tmp/donors
awk -F'[: ]' '$3~/(916)/{print}' /tmp/donors
字段等于长度条件 打印
awk 'length($1)==4 {print}'
格式化 输出
awk -F: '/Mike/{printf "$%s $%s $%s/n",$3,$4,$5} /tmp/donors'
awk -F'[: ]' '{printf "%s,%s:%s %s:%s:%s:%s/n",$1,$2,$3,$4,$5,$6,$7}' /tmp/donors
字段条件 打印
awk -F'[: ]' '$6 > 100 {print $1, $2}' /tmp/donors
awk -F'[: ]' '$7 < 85 {print $1, $2, $4}' /tmp/donors
awk -F'[: ]' '($5>=75)&&($5<=150){print $1, $2, $4}' /tmp/donors
awk -F'[: ]' '($5+$6+$7 > 800){print $1, $2}' /tmp/donors
awk -F'[: ]' '(($5+$6+$7)/3 > 200){print $1, $2}' /tmp/donors
不等于条件 打印
awk -F'[: ]' '!/(916)/{print $1}' /tmp/donors
widows文件转unix
windows文件换行是\r\n,unix只是\n,所以windows文件放在unix中查找’d$'会查找不到
sed -i 's/\r//g' dos.txt
dos2unix dos.txt
正则
语法
- .
代表一个任意字符
a…
al e - *
前一个字符出现任意多次
xy*表示 x,xy,xyyy… - ?
前一个字符出现0次或者1次
abc?表示ab或者abc
ls ab?表示的不一致 - +
前一个字符至少出现一次
xy+表示xy,xyy… - []
代表字符集合[ab][a-z][0-9] - [^]
不匹配字符集合[^0-9] - ^
找一行开始 - $
一行结尾
3$ - \*
转义 - /reg/
正则表达式分隔符 - \<
匹配单词的开始 \<a 匹配所有a开头的单词 - \>
匹配单词的结尾 \<a 匹配所有以a结尾的单词 - {}
前一个字符重复出现,o{3},匹配o出现3的次, {1,3}出现1到3次,{1,}至少出现一次, - ()
grouping
BRE 基础正则
ERE 扩展正则
编码问题:中文[A-Z] AaBb… 包含小写
LANG=C转换为asc编码解决
文件流和管道
重定向
输入重定向
<
输出重定向
>
例子:
tr 被替换字符 替换字符 默认是从键盘输入 输出到显示屏上
tr abc 123 < /tmp/f1 > ./f2
标准错误输出重定向
find /tmp -name ‘xxxx*’ 会有权限不够的错误提示,我们希望忽略错误信息
2>
例子
/dev/null 文件大小一直为0
find /tmp -name '*.c' 2>/dev/null
区分标准输出和标准错误输出
find /tmp -name '*.c' > res 2> error
标准输出和标准错误一起保存
>&
1.Locate the foobar file in your home directory and
save its absolute pathname in the foobar.path file.
Append error messages to /dev/null. Show your session.
find /tmp -name "foo*" > ~/foobar.path 2> /dev/null
2.Combine data in the following files (in this order)
and append it to the all.labs file: lab1, lab2, lab3, and lab4.
Any errors should be redirected to the error.log file. Show your session.
paste lab1 lab2 lab3 lab4 > ~/all.labs 2> error.log
管道
一条命令执行完后,并不能满足要求,需要下一条命令跟进处理
Linux设计的KISS原则,keep it simple and stupid,每条命令是简单好用的,那为了处理复杂的任务需要用
使用 |
例子
who | wc
who | grep -c '188*'
查看重复登录的人
who | awk '{print $1}' | sort | uniq -c
找系统空间占用最大的前5个文件
sudo du -s ~/* | sort -k1 -nr | head -5
3.Use a command line to display the records in the file
student_record for the top five students in ascending (sorted) order,
i.e., with the highest GPA student’s record displayed first.
Show your session.
sort -k4 -nr /tmp/student_record |head -5
4.How many directory are there in the your home directory. Show the command.
ls -l |grep "^d"|wc -l
文件共享
创建连接 ln
echo hello > chapter3
ln chapter3 chapter3.hard
cat chapter3.hard
和 cat chapter
是一致的,访问的同一个文件,添加和修改任一文件都同步
-s
创建软连接
硬链接
硬链接是直接建立在节点表上的(inode),建立硬连接指向一个文件的时候,会更新节点表上面的计数值。硬链接直接使用inode号作为文件指针(52473)。
软链接
它是指向另一个文件的特殊文件,这种文件的数据部分仅包含它所要链接文件的路径名。软链接是为了克服硬链接的不足而引入的,软链接不直接使用inode号作为文件指针,而是使用文件路径名作为指针(软链接:文件名 + 【数据部分–>目标文件的路径名】)。软件有自己的inode,并在磁盘上有一小片空间存放路径名。因此,软链接能够跨文件系统,也可以和目录链接。
期中测试题
##########################################################
II. Short Answer Questions (3 * 10 = 30 points)
Anser the questions according to the file /tmp/donors
##########################################################
Delete all the repeated lines.
ANS: awk ‘!i[$1]++’ log
input your answer:
Print all the lines containing “Main”
ANS:
input your answer: awk ‘/Main/’ /tmp/donors
Print all the names whose first month donation and second month donation are both less than 100
ANS:
input your answer: awk -F: ‘$3 < 100 && $4 < 100 {print $1}’
Delete all the blank line.
ANS:
input your answer: sed ‘/^ *$/d’ a
Print all the names second month donation is greater than 150
ANS:
input your answer: awk -F: ‘$4>150 {print $1}’ /tmp/donors
Save all the lines where the person’s last name starts with “D” to file named “result”
ANS:
input your answer: awk ‘$2~/D/{print}’ /tmp/donors > result
Print all the lines starting with “J”
ANS:
input your answer: grep ‘^J’ /tmp/donors
Print how many person whose fist montn donation is greater than 200
ANS:
input your answer: awk -F: ‘$3>200 {print}’ /tmp/donors|wc -l
Replace all the string “(206)” to “(306)”.
ANS:
input your answer:sed ‘s/(206)/(306)/g’ /tmp/donors
Sort the person according to their first month donation.
ANS:
input your answer: sort -t: -k3 -n /tmp/donors
进程
进程状态
ps
显示现在使用的进程
ps - report a snapshot of the current processes.
显示所有进程
ps -e
长(完整)显示
ps -l
PID(process ID) 、PPID(parent process ID)可以查看
top
实时显示进程
pstree
pstree - display a tree of processes
进程控制
jobs
查后台任务
查看进程号 -l
jobs -l
kill -9 pid
cmd &
后台执行
fg [%num]
切换至前台运行
bg [%num]
切换至后台运行
ctrl + z
进程停止
ctrl + c
进程终止
后台进程终止; fg %num + ctrl c
或者用 kill -9命令
kill
kill - send a signal to a process
内部发送一个信号量给进程
kil -l
查看所有信号量
如越界,除数为0,内部发送信号给进程
语法:
kill [ -signal | -s signal ] pid ...
kill [ -L | -V, --version ]
kill -l [ signal ]
进程终止 -9
kill -9 pid
jobs -l 查看pid
串行;并行&执行cmd
nohup
进程不挂起,bash退出继续运行
nohup + cmd
at
一次性定时任务
crontab
周期定时任务
-
在 12:01 a.m 运行,即每天凌晨过一分钟。这是一个恰当的进行备份的时间,因为此时系统负载不大。
1 0 * * * /root/bin/backup.sh
-
每个工作日(Mon – Fri) 11:59 p.m 都进行备份作业。
59 11 * * 1,2,3,4,5 /root/bin/backup.sh
下面例子与上面的例子效果一样:
59 11 * * 1-5 /root/bin/backup.sh
-
每5分钟运行一次命令
*/5 * * * * /root/bin/check-status.sh
-
每个月的第一天 1:10 p.m 运行
10 13 1 * * /root/bin/full-backup.sh
-
每个工作日 11 p.m 运行。
0 23 * * 1-5 /tmp/backup.sh
网络
ifconfig [ethX]
ip addr show
/etc/network/interfaces
配置网卡文件
配置后需要重新启动
ifdown ethX
ifup ethX
管理网卡指令
route
显示ip路由表
netstat
netstat -rn 等同于 route
netstat -an
显示监听接口
ping
-c num
-s size
ping -c 3 -s 64 222.201.101.1
traceroute
taceroute ip
/etc/hosts
配置域名-ip
nslookup
nslookup ip
查看域名-ip
shell编程
shell script
nano first.sh
输入 echo __
修改权限
chmod 755 first.sh
立即生效
source
source: 用法: source filename [arguments]
source等同于./
终端进程直接执行script里面的执行指令
source first.sh
bash
Bash is an sh-compatible command language interpreter that executes commands read from the standard input or from a file.
开启一个新进程bash shell执行script里面的指令
bash first.sh
直接执行文件
first.sh
首先会找#!,确定interpreter指示器,再执行指令
#!/bin/bash
linux中有不同的shell,各自语法不一样,加上!/bin/bash,确定语法
创建变量
name="tom"
echo $name
自定义变量,只能在当前进程中使用,环境变量则是全局的,环境变量会作为参数传递到子进程中
export name
自定义变量转为环境变量
自定义的变量和环境变量是两个不同的副本
name=“jack”
修改自定义变量不会改变环境变量
深修改:source or . ./
read only shell variables
./fist.sh a b c
$0 name of program
$1 a
$1-$9 values of argument of commands line
$* “a b c”
$@ “a” “b” “c”
$# number of the variables
$$ 进程id
$? 系统返回值,可以根据返回值作为错误提示
删除变量
unset variable
常量
readonly variable
输入输出入
read i [variable list]
echo $i
read -p "please input :" name
read 指令默认用空格区分 赋值给var list,当到最后一个var时赋值为剩下的所有字符串。
echo "$i"
默认添加换行符
echo -n $i
不添加换行符
echo -e 转义字符生效
shift
command in shell script
shift [N]
N is a number, default is 1.
var list index fixed, and var value left shift N
感觉没啥用,
参数遍历,
处理完$1,使用shift,参数2变为$1
set
set [option] [var list]
set a b c
$1 = a
$2 = b
$3 = c
提取字段
[s @ Linux ~] size=`ls -l /etc/passwd | awk '{print $5}'`
>>> 112590
法2:
[s @ Linux ~] set -- `ls -l /etc/passwd`
[s @ Linux ~] echo $5
>>> 112590
用例
[s @ Linux ~] $ set -- `df /var | tail -1`
[s @ Linux ~] $ echo $5
59%
[s @ Linux ~] $ echo $5 | sed 's/%//'
59
for循环
for 语句;do
done
for file in `ls $file_name`;do
set -- `ls -l $file_name/$file`
size=$5
if [ $size -eq 0 ];then
echo "$file : length is zero, delete it"
rm $file_name/$file
fi
done
shell脚本编程简单例子
例子1
打印文件名,大小,所有者,硬链接数,更新时间,如果文件大小为0将它删除
#!/bin/bash
# read file_name
if [ $# -ne 1 ]
then
echo "wrong usage, usage: $0 name"
exit 1
fi
file_name=$1
if [ -d $file_name ]
then
echo "error " $file_name "is dir"
elif [ -f $file_name ]
then
echo $file_name
set -- `ls -l $file_name`
size=$5
owner=$3
hard_links_num=$2
update_time=`stat -c %y $file_name`
echo "file name is $file_name"
echo "file size is $size"
echo "the number of hard links is $hard_links_num"
echo "file owner: $owner"
echo "modify date: $update_time"
if [ $size -eq 0 ];then
echo "file size is zero, delete it"
rm $file_name
fi
else
echo "can't find the file or directory: $file_name"
fi
例子2
打印目录下的所有文件,如果文件大小为0将它删除。
#!/bin/bash
# read file_name
if [ $# -eq 0 ];then
file_name=.
elif [ $# -eq 1 ];then
file_name=$1
else
echo "error usage! 12_10.sh [dir_name] "
fi
if [ -f $file_name ]
then
echo "error!" $file_name "is file, please input a dir name"
elif [ -d $file_name ]
then
for file in `ls $file_name`;do
set -- `ls -l $file_name/$file`
size=$5
if [ $size -eq 0 ];then
echo "$file : length is zero, delete it"
rm $file_name/$file
fi
done
else
echo "can't find the dir $file_name"
fi
例子3
option argument programming
#!/bin/bash
# read file_name
if [ $# -eq 0 ];then
file_name=.
elif [ $# -eq 1 ];then
file_name=$1
else
echo "error usage! 12_10.sh [dir_name] "
fi
if [ -f $file_name ]
then
echo "error!" $file_name "is file, please input a dir name"
elif [ -d $file_name ]
then
for file in `ls $file_name`;do
set -- `ls -l $file_name/$file`
size=$5
if [ $size -eq 0 ];then
echo "$file : length is zero, delete it"
rm $file_name/$file
fi
done
else
echo "can't find the dir $file_name"
fi
例子4
#!/bin/bash
# read file_name
if [ $# -eq 2 ];then
int1=$1
int2=$2
else
echo "error usage! 13_6.sh integer1 integer2"
fi
if [ $int1 -lt $int2 ];then
i=$int1
let end_i=int2+1
while [ $i -ne $end_i ]
do
echo $i
let i=i+1
done
fi
if [ $int1 -gt $int2 ];then
i=$int1
let end_i=int2-1
while [ $i -ne $end_i ]
do
echo $i
let i=i-1
done
fi
例子5
#!/bin/bash
# read file_name
if [ $# -eq 0 ];then
echo "error usage! 13_11.sh integer1 integer2..."
exit 1
fi
sum=0
for i in $@
do
let out=i*i
let sum=sum+out
echo $out
done
echo $sum