Shell Script Small Example

Source: Internet
Author: User

1-100 the sum of the natural numbers: Law one: For Loop #!/bin/bash#declare-i sum=0for ((i=0;i<=100;i++));d o let sum+= $idoneecho "sum: $sum" Law two: While loop #!/bin/bash#declare-i sum=0declare-i i=0while [$i-le];d o let sum+= $i let I++doneecho $iecho "Summary: $sum. " Script for Statistics TCP connection Status: #!/bin/bash# This script was Count TCP statusdeclare-i estab=0declare-i listen=0declare-i other=0for State  In $ (Netstat-tan | grep "^tcp\>" | awk "{print $NF}");d o if ["$state" = = ' established '];then let estab++ elif [ "$state" = = ' LISTEN '];then let listen++ else let other++ Fidoneecho "established: $estab" echo "LISTEN: $listen" Ech  O "Unknow: $other" Add 10 users in bulk: #!/bin/bashif [! $UID-eq 0];then echo "Only the root can use this script." Exit 1fifor i in {1..10};d o if ID user$i &>/dev/null;then echo "user$i exist" else useradd user$i if [ $? -eq 0];then echo "user$i" | passwd--stdin user$i &>/dev/null fi fidone test connectivity scripts for hosts within a network segment: #!/bin/bash#ping net= ' 172.16.1 ' uphosts=0downhosts=0 For I in {1..254};d o ping-c 1-w 1 ${net}.${i} &>/dev/null if [$?-eq 0];then echo "${net}.${i} is up."    Let uphosts++ else echo "${net}.${i} was down."  Let downhosts++ Fidoneecho ' up hosts: $uphosts ' echo ' Down hosts: $downhosts ' factorial of the Count: #!/bin/bash#fact () {if [$1-eq 0-o $                           -eq 1];then Echo 1 else echo $[$1*$ (fact $[$1-1]) Fi}fact 5

  

Shell Script Small Example

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.