shell基礎練習<一>

來源:互聯網
上載者:User

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學習之路” 部落格

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.