shell指令碼練習

2021-08-26 20:28:36 字數 4751 閱讀 4107

顯示系統資訊

#!/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]\.)[0-9]'|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]\.)[0-9]'|cut -d" " -f2`"

echo "my username is $user"

echo "my hostname is `hostname`"

cat > /etc/profile.d/env.sh 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 <#!/bin/bash

#author $name

#qq $qq

#date `date +%f`

#filename $1

#url

eofchmod +x $1

vim + $1

1、編寫指令碼/bin/per.sh,判斷當前使用者對指定引數檔案,是否不可讀並且不可寫

2、編寫指令碼/root/bin/excute.sh ,判斷引數檔案是否為sh字尾的普通檔案,如果是,新增所有人可執行許可權,否則提示使用者非指令碼檔案

#!/bin/bash

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

[! -f $1]&&

[[ "$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

#date: 2018-08-29

#filename: iftest.sh

#url:

#description: the test script

read -p "please input your age: " age

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

true

else

echo "please input a digit"

exit 10

fiif [ "$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"

fiif 判斷年齡

#!/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

fiif [ "$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"

fiif 判斷yes no

#!/bin/bash

#author: wangxiaochun

#date: 2018-08-29

#filename: yesorno.sh

#url:

#description: the test script

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

#date: 2018-08-29

#filename: yesorno2.sh

#url:

#description: the test script

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

shell 指令碼練習

bin bash echo hello world chmod x test.sh 使指令碼具有執行許可權 test.sh 執行指令碼for file in ls etc 或for file in ls etc val expr 2 2 注意,這時的計算,2 2運算子和數字之間一定要加空格判斷字串是...

shell指令碼練習

1.簡單輸出指令碼 輸出 hello 王子 我們先建立指令碼檔案 touch lian.sh 這裡指令碼的檔案需要以。sh結尾 結果 2.通過位置變數建立系統賬號或密碼 結果 3.每週 5 使用 tar 命令備份 var log 下的所有日誌檔案 vim root logbak.sh 編寫備份指令碼...

Shell指令碼練習

一 將陣列 45,86,28,85,15,19,56,8 加起來 bin bash arr1 45 8628 8515 19568 sum 0 for i in dosum i sum done echo sum 二 寫乙個指令碼 執行指令碼時 傳入 幾個 數字 將傳入的數字中最大的那個輸出出來 b...