shell基础练习<一>


1、要求用户输入姓名,年龄,性别

#!/bin/bash

read -p "Please input your name:" name

read -p "Please input your age: " age

read -p "Please input your sex:" sex

echo "Your name is $name,age is $age,sex is $sex."

2、写一个脚本arvg.sh,可以读取用户输入的参数变量,如sh arvg.sh test12 ,然后问用户是否要创建目录,

如果用户回答yes或者YES,则为用户创建目录,然后问用户是否为其创建文件,如果回答yes,则在刚才已经

创建好的目录下创建test01。

#!/bin/bash

if [ $#  -lt 1 ];then

    echo "Please like 'sh  test.sh a b c'"

fi

read -p  "are you mkdir file?Please enter yes/YES or no:" qq

if [ $qq == yes ] || [ $qq == YES];then

    mkdir  $1

    for i in $(seq 1 10);

    do

        touch $1/test$i

    done

else    echo "No file created"

    exit 0

fi

3、随机更改密码:

#!/bin/bash

#2011/07/10 by larry

LOG=PassChange`date +%F`.log

> $pass

cat /etc/passwd | while read line

do

    id=`echo $line | awk -F":" '{print $3}'`

    user=`echo $line | awk -F":" '{print $1}'`

    PASS=$RANDOM

    if [ $id -lt 500 ];then

            echo "$user is system users."

            elif [ $id -gt 500 ];then

               echo $RANDOM | passwd $user --stdin

            echo "$USER $id $RANDOM" >> $LOG

    fi

done

#####################################

#!/bin/bash

#2011/07/10

now=`date +%F`

file=/etc/passwd

for user1 in `cat $file | awk -F: '$3<500{print $1}'`

do

    echo $user1 is systerm user

done

for user2 in `cat $file | awk -F: '$3>500{print $1}'`

do

    echo $RANDOM |passwd $user2 --stdin  >> /dev/null

        echo "$user2 $RANDOM" >> Pass$now.log

done

本文出自 “Larry学习之路” 博客

相关内容

    暂无相关文章