shell脚本练习

显示系统信息

#!/bin/bash

#Descrition: show system information

#

echo "OS version is:`cat /etc/centos-release`"

echo "kernel version is `uname -r`"

echo "CPU type:`lscpu|grep "Model name"|tr -s " " |cut -d: -f2`"

cat /proc/meminfo |head -n1

echo "Disk:`lsblk|grep '^sd'|tr -s " " |cut -d" " -f1,4`"

echo "Network IP:`ifconfig eth0|grep -Eo 'inet ([0-9]{1,3}\.){3}[0-9]{1,3}'|cut -d" " -f2`"

echo "My username is $USER"

echo "My hostname is `hostname`"

 

显示系统信息 进阶

#!/bin/bash

#Descrition: show system information

#

echo "OS version is:\[\e[1;31m`cat /etc/centos-release`\[\e[0m\]"

echo "kernel version is `uname -r`"

echo "CPU type:`lscpu|grep "Model name"|tr -s " " |cut -d: -f2`"

cat /proc/meminfo |head -n1

echo "Disk:`lsblk|grep '^sd'|tr -s " " |cut -d" " -f1,4`"

echo "Network IP:`ifconfig eth0|grep -Eo 'inet ([0-9]{1,3}\.){3}[0-9]{1,3}'|cut -d" " -f2`"

echo "My username is $USER"

echo "My hostname is `hostname`"

*******************************************************

 

cat > /etc/profile.d/env.sh <<EOF

PS1='\[\e[1;35m\][\u@\h \W]\$\[\e[0m\]'

alias name=hostname

EOF

 

创建 apache 用户 gid uid 为80 家目录 为 /var/www 默认shell sbin/nologin

#!/bin/bash

groupadd -g 80 apache

useradd -u 80 -g apache -r -d /var/www/ -s /sbin/nologin apache

echo "User apache is created"

mkdir /var/www/

chown apache.apache /var/www/

echo "/var/www is ready!"

 

创建用户首次登录更改口令

#!/bin/bash

Id $1 &&echo "user $1 exist" &&exit

useradd $1 #为用户增加密码

echo magedu | passwd --stdin $1 #强制第一次登陆改密码

chage -d 0 $1

 

自动添加信息 魔数

#!/bin/bash

#

#------------

name=luoweijun

qq=601589756

cat > $1 <<EOF

#!/bin/bash

#Author $name

#QQ $qq

#Date `date +%F`

#FileName $1

#URL http://www.magedu.com

 

EOF

chmod +x $1

vim + $1

1、编写脚本/bin/per.sh,判断当前用户对指定参数文件,是否不可读并且不可写

2、编写脚本/root/bin/excute.sh ,判断参数文件是否为sh后缀的普通文件,如果是,添加所有人可执行权限,否则提示用户非脚本文件

#!/bin/bash

[-z "$1"] && echo "input afile"

[! -f $1]&& { echo "$1is not a norma file" ; exit; }

[[ "$1"=~ [^.].*\.sh ]] && chmod +x $1

echo "all users can excutr $1"

3、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统

如果用户user6不存在,就添加用户user6

! id user6 && useradd user6

id user6 || useradd user6

如果/etc/inittab文件的行数大于100,就显示好大的文件;

[ `wc -l /etc/inittab | cut -d' ' -f1` -gt 100 ] && echo "Large file."

如果用户存在,就显示用户已存在;否则,就添加此用户;

id user1 && echo "user1 exists." || useradd user1

如果用户不存在就添加;否则,显示其已经存在;

! id user1 && useradd user1 || echo "user1 exists."

如果用户不存在,添加并且给密码;否则,显示其已经存在;

! id user1 && useradd user1 && echo "user1" | passwd --stdin user1 || echo "user1 exists."

1. if 判断年龄

#!/bin/bash

#

#********************************************************************

#Author: wangxiaochun

#QQ: 29308620

#Date: 2018-08-29

#FileName: iftest.sh

#URL: http://www.magedu.com

#Description: The test script

#Copyright (C): 2018 All rights reserved

#********************************************************************

read -p "Please input your age: " age

if [[ "$age" =~ ^[[:digit:]]+$ ]] ;then

true

else

echo "Please input a digit"

exit 10

fi

if [ "$age" -ge 0 -a "$age" -le 18 ];then

echo "good good study,day day up"

elif [ "$age" -le 60 ];then

echo "work hard"

elif [ "$age" -le 120 ];then

echo "enjoy your life"

else

echo "you don not come from the earth"

fi

 

------------------------------------------------------------------------------

if 判断年龄

#!/bin/bash

#Author zhanghongwen

#Date 2018-08-29

#Name score.sh

read -p "please input your score " score

if [[ "$score" =~ ^[[:digit:]]+$ ]] ; then

true

else

echo "please input digit"

exit 10

fi

if [ "$score" -ge 0 -a "$score" -lt 60 ];then

echo "please work more hard"

elif [ "$score" -ge 60 -a "$score" -lt 80 ];then

echo "please work hard"

elif [ "$score" -ge 80 -a "$score" -le 100 ];then

echo "your are a goog man"

else

echo "your score is false"

fi

---------------------------------------------------------------------------------

if 判断yes no

#!/bin/bash

#

#********************************************************************

#Author: wangxiaochun

#QQ: 29308620

#Date: 2018-08-29

#FileName: yesorno.sh

#URL: http://www.magedu.com

#Description: The test script

#Copyright (C): 2018 All rights reserved

#********************************************************************

read -p "Input yes or no:" answer

ans=`echo $answer |tr 'A-Z' 'a-z'`

if [ "$ans" = "yes" -o "$ans" = "y" ];then

echo "YES"

elif [ "$ans" = "no" -o "$ans" = "n" ];then

echo "NO"

else

echo "Please input yes or no"

fi

---------------------------------------------------------------------------

#!/bin/bash

#

#********************************************************************

#Author: wangxiaochun

#QQ: 29308620

#Date: 2018-08-29

#FileName: yesorno2.sh

#URL: http://www.magedu.com

#Description: The test script

#Copyright (C): 2018 All rights reserved

#********************************************************************

read -p "Input yes or no:" answer

if [[ "$answer" =~ ^[Yy]([Ee][Ss])?$ ]] ;then

echo YES

elif [[ "$answer" =~ ^[Nn][Oo]?$ ]] ;then

echo NO

else

echo "Please input yes or no"

fi

-----------------------------------------------------------------------------

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值