码迷,mamicode.com
首页 > 系统相关 > 详细

学习日志---linux打卡11

时间:2015-10-16 10:14:31      阅读:354      评论:0      收藏:0      [点我收藏+]

标签:linux

练习:写一个脚本

1、让用户交互式输入一个用户名,先判定用户是否存在;不存在,则以7为退出码;

    2、判断用户的shell是否为/bin/bash;如果是,则显示为“Bash User.”,退出码为0; 否则,则显示为“Not Bash User.”,退出码为1;


#!/bin/bash

#

read -p "Enter a user name: " userName

if ! id $userName &> /dev/null; then

 echo "No such user."

 exit 7

fi      


userShell=`grep "^$userName\>" /etc/passwd | cut -d: -f7`


if [[ "$userShell" == "/bin/bash" ]]; then

 echo "bash user."

 returnValue=0

else

 echo "Not bash user."

 returnValue=1

fi


exit $returnValue



练习:写一个脚本

1、显示如下菜单:

cpu) show cpu info;

mem) show memory info;

quit) quit

Enter your option: CPU Cpu, CpU

2、如果用户选择cpu,则显示文件/proc/cpuinfo的信息;

3、如果用户选择mem,则显示文件/proc/meminfo的信息;

4、如果用户选择quit,则退出,且退出码为5;

5、如果用户键入其它字符,则显示未知选项,请重新执行脚本;退出码为6;


#!/bin/bash

#

returnValue=0


cat << EOF

cpu) print cpu infomation

mem) print meory infomation

quit) Quit

EOF


read -p "Enter your option: " userOption

userOption=`echo $userOption | tr ‘A-Z‘ ‘a-z‘`        注意使用转义


if [[ "$userOption" == "cpu" ]]; then                 这里的[[]]也可以使用[],只有模式匹配用[[]]    整数测试[]

 cat /proc/cpuinfo

elif [[ "$userOption" == "mem" ]]; then

 cat /proc/meminfo

elif [[ "$userOption" == "quit" ]]; then

 echo "quit"

 returnValue=6

else

 echo "unkown option"

 returnValue=7

fi


exit $returnValue




回顾:

字符测试:

双目:

>

<

==

=~

!=, <>

单目:

-n

-z


vim:

编辑、输入、末行

vim +# file


光标跳转:h, j, k, l

 w, b, e

 0, ^, $

 ), (

 {, }

 G, #G

编辑命令:

x, r

d, 

y,  .,+10y 复制当前到后面十行

c,

p,P

u

.

Ctrl+r


vim可视化模式:

v: 按光标走过的区域选择

V: 选择矩形块


打开多个文件,分窗口:

vim -o

vim -O

Ctrl+w(walk), arrow


分割当前窗口:

Ctrl+w, s

Ctrl+w, v


窗口属性的定义:

:set nu   显示行号

:set nonu


:set ai   自动缩进 auto indent

:set noai


:set ic   查找时忽略大小写 ignore case

:set noic


显示对应的括号

:set sm (show match)

:set nosm


语法高亮:

:syntax on

:syntax off


搜索高亮:

:set hlsearch

:set nohlsearch


vim的配置文件:

全局:/etc/vimrc

个人:~/.vimrc


查找替换:s是替换的意思substitute

:地址定界s/查找模式/替换为的内容/gi

g: global 全局替换

i: ignore-case


:地址定界s@查找模式@替换为的内容@gi   分隔符可以自定义

    例如这个: .,+3s@bin@BIN@g

    当前行开始到下面第三行做替换 把bin替换成大写BIN,每行全部出现都替换g

    全文查找替换的话就是 %s 或者 1,$s


&: 用于在替换为的内容部分中引用前面匹配到的所有内容;

例子: .,+3s@^[^#]@#&@g

当前行到第三行  开头非#的行前面都加上#   &代表前面匹配到串


:.,+2s@\(\<l..e\>\).*\1@&r@g 


练习:

1、复制/etc/grub.conf至/tmp目录,删除/tmp/grub.conf文件中行首的空白符;

%s@^[[:space:]]\{1,\}@@g

这里注意要一个或者多个


2、复制/etc/rc.d/rc.sysinit至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行,行首加#号

:%s@^[[:space:]]\{1,\}.*@#&@


3、删除/tmp/rc.sysinit文件中以#开头且后面跟了至少一个空白字符的行的行首的#号和空白符;

:%s@^#[[:space:]]\{1,\}@@g


4、为/tmp/grub.conf文件中前三行的行首加#号; 

1,3s@^@#@g


5、将/etc/yum.repos.d/CentOS-Media.repo中enable=0一行最后的0改为1;

%s@\(enabled=\)[0-9]@\11@g


%s是全文搜索





算术计算

条件判断

磁盘分区、格式化




172.16.0.0/255.255.0.0


1/101



0,100



文件描述符:文件描述符在形式上是一个非负整数。实际上,它是一个索引值,指向内核为每一个进程所维护的该进程打开文件的记录表。当程序打开一个现有文件或者创建一个新文件时,内核向进程返回一个文件描述符。


请参见:http://zh.wikipedia.org/wiki/%E6%96%87%E4%BB%B6%E6%8F%8F%E8%BF%B0%E7%AC%A6




bash知识点:文件测试,判断文件的属性

[]

[[]]

test


单目测试:

-e /path/to/file: 测试文件是否存在;  exist

[ -e file ] || mkdir file   如果文件存在则不执行,如果文件不存在则创建目录

-a /path/to/file: 测试文件是否存在;

-f /path/to/file: 测试是否为普通文件;

-d /path/to/somefile: 测试是否为目录文件;

-b /path/to/somefile: 测试文件是否存在并且是否是一个块设备文件; 

-c /path/to/somefile:                               字符设备文件;

-h|-L /path/to/somefile: 测试文件是否存在并且为符号链接文件:

-p /path/to/somefile:                       管道文件;  

-S /path/to/somefile:                        套接字文件; 

-r /path/to/somefile: 测试其有效用户是否对此文件有读取权限;

-w                                                写权限

-x                                                执行权限

-s /path/to/somefile: 测试文件是否存在并且不空,  size


双目测试:

file1 -nt file2: 测试file1是否为file2更 新一些;



      -a file

              True if file exists.

       -b file

              True if file exists and is a block special file.

       -c file

              True if file exists and is a character special file.

       -d file

              True if file exists and is a directory.

       -e file

              True if file exists.

       -f file

              True if file exists and is a regular file.

       -g file

              True if file exists and is set-group-id.

       -h file

              True if file exists and is a symbolic link.

       -k file

              True if file exists and its ?..ticky?..bit is set.

       -p file

              True if file exists and is a named pipe (FIFO).

       -r file

              True if file exists and is readable.

       -s file

              True if file exists and has a size greater than zero.

       -t fd  True if file descriptor fd is open and refers to a terminal.

       -u file

              True if file exists and its set-user-id bit is set.

       -w file

              True if file exists and is writable.

       -x file

              True if file exists and is executable.

       -O file

              True if file exists and is owned by the effective user id.

       -G file

              True if file exists and is owned by the effective group id.

       -L file

              True if file exists and is a symbolic link.

       -S file

              True if file exists and is a socket.

       -N file

              True if file exists and has been modified since it was last read.

       file1 -nt file2   比较1是否比2要新

              True if file1 is newer (according to modification date) than file2, or if file1 exists and file2 does not.

       file1 -ot file2

              True if file1 is older than file2, or if file2 exists and file1 does not.

       file1 -ef file2

              True if file1 and file2 refer to the same device and inode numbers.

              是否引用同一个设备文件


在脚本中使用source指令,可以把source后的文件读入到当前进程中,作为当前进程的数据变量,.和source的作用一样


写一个脚本,完成如下任务:

1、分别复制/var/log下的文件至/tmp/logs/目录中;

2、复制目录时,才使用cp -r

3、复制文件时,使用cp

4、复制链接文件,使用cp -d

5、余下的类型,使用cp -a


#!/bin/bash

#

targetDir=‘/tmp/logs‘


[ -e $targetDir ] || mkdir $targetDir    如果目录不存在就先创建


for fileName in /var/log/*; do

 if [ -d $fileName ]; then

   copyCommand=‘cp -r‘

 elif [ -f $fileName ]; then

   copyCommand=‘cp‘

 elif [ -h $fileName ]; then

   copyCommand=‘cp -d‘

 else

   copyCommand=‘cp -a‘

 fi  


 $copyCommand $fileName $targetDir

done


cp -d 是复制链接文件

cp -a 是复制文件并保留原有属性


写一个脚本,完成如下任务,其使用形式如下所示:

script.sh {start|stop|restart|status}

其中:

如果参数为空,则显示帮助信息,并退出脚本;

如果参数为start,则创建空文件/var/lock/subsys/script,并显示“starting script successfully.”

如果参数为stop,则删除文件/var/lock/subsys/script,并显示“Stop script successfully.”

如果参数为restart,则删除文件/var/locksubsys/script并重新创建,而后显示“Restarting script successfully.”

如果参数为status,那么:

如果文件/var/lock/subsys/script存在,则显示“Script is running...”,否则,则显示“Script is stopped.”



bash的知识点:位置参数轮替(shift)


#!/bin/bash

#


declare -i sum=0            求输入参数的和


for i in `seq 1 $#`; do     $#可以得到参数的个数

 let sum+=$1               这里始终使用位置参数1

 shift                     shift实现轮替

done


echo $sum



写一个脚本:使用形式如下

userinfo.sh -u username [-v {1|2}]

-u选项用于指定用户;而后脚本显示用户的UID和GID;

如果同时使用了-v选项:

-v后面的值如果是1, 则额外显示用户的家目录路径;

-v后面的值如果是2, 则额外显示用户的家目录路径和shell;


#!/bin/bash

#

[ $# -lt 2 ] && echo "Too less argements, quit" && exit 3


if [[ "$1" == "-u" ]];then

 userName="$2"

 shift 2

fi


if [ $# -ge 2 ] && [ "$1" == "-v" ]; then

 verFlag=$2

fi


verFlag=${verFlag:-0}


if [ -n $verFlag ];then

 if ! [[ $verFlag =~ [012] ]];then

   echo "Wrong parameter."

   echo "Usage: `basename $0` -u UserName -v {1|2}"

   exit 4

 fi  

fi


# echo $userName $verFlag


if [ $verFlag -eq 1 ]; then

 grep "^$userName" /etc/passwd | cut -d: -f1,3,4,6

elif [ $verFlag -eq 2 ];then 

 grep "^$userName" /etc/passwd | cut -d: -f1,3,4,6,7

else

 grep "^$userName" /etc/passwd | cut -d: -f1,3,4

fi


学习日志---linux打卡11

标签:linux

原文地址:http://wukong0716.blog.51cto.com/10442773/1703406

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!