Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Making random numbers and nested loops with Shell

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces in detail the method of making random numbers and nested loops in Shell. The sample code in this article is very detailed. Zero basis can also refer to this article. Interested friends can refer to it.

1. Random number 1. How to generate random numbers?

System variable: RANDOM, which produces a random integer of 0,32767 by default

To call a variable, you have to pay no matter what variable you are, and it is in US dollars: heavy_dollar_sign:

Print a random number echo $RANDOM to view the last random number generated by the system # set | grep RANDOMRANDOM=28325 generates random numbers between 01s echo $[$RANDOM%2] generate random numbers echo $[$RANDOM%3] generate random numbers echo $[$RANDOM%4] generate random numbers echo $[$RANDOM] generate random numbers echo $[$RANDOM1] generate random numbers echo $[$RANDOM1] generate random numbers within 50-100 random numbers echo $[$RANDOM%51+50] generate three random numbers The random number of digits echo $[$RANDOM%900+100] 2. Actual combat case 1) randomly generate phone numbers that begin with 139

Specific requirements 1:

Write a script, generate a phonenum.txt file, and randomly generate 1000 mobile phone numbers starting with 139, each line.

The ① idea generates 1000 phone numbers. The script needs to cycle 1000 FOR WHILE UNTIL139+8 digits, and the last 8 digits are randomly generated. Each digit can be randomly generated echo $[$RANDOM] save the randomly generated digits into variables, and then add 139to the file to save ② to achieve #! / bin/env bash# to generate 1000 phone numbers starting with 139and save the file phonenum.txtfile=/shell03/phonenum.txtfor ( I > $filedonestrings binapash random phonenum# cycle 1000 times to generate phone numbers and save them to the file for i in {1... 1000} do n1cards $[RANDOM] n2cards $[RANDOM] n3cards $[RANDOM] n4eggs $[RANDOM] n5cards $[RANDOM] n7cards $[RANDOM] n8cards $[RANDOM] echo "139 $N1 $n2 $n3 $n4 $N5 $N6 $N7 $N8" > phonetic txtboxes balance while [$I-le] do n1cards $[$RANDOM] n2cards $[$RANDOM] N3 million $[$RANDOM] n4 percent $[$RANDOM] n5 percent $[$RANDOM] n6 percent $[$RANDOM] n7 percent $[$RANDOM] n8 percent $[$RANDOM] echo "139 $N1 $n2 $n3 $n4 $n5 $n6 $n7 $N8" > phonenum.txt let i++donecontinue: continue Skip this cycle and execute the next cycle break: interrupt, execute the extracorporeal code outside do..done exit: exit program #! / bin/bashfor I in {1.. 1000} do n1 steps $[$RANDOM] n2 steps $[$RANDOM] n3 cycles $[$RANDOM] n4 cycles $[$RANDOM] n6 cycles $[$RANDOM] n7 cycles $[$RANDOM] n8 cycles $[$RANDOM] echo "139 $N1 $N2 $n3 $n5 $N6 $N7 $N8" > phoneme. Txtshipping exercise binder bashrooms create phonenum filefor (icycle 1) I luck.txt # Delete the extracted lucky viewer number # sed-I "/ $luck_num/d" $phonedone viewers / binder bashfileholders * & echo $luck > > / shell04/luck_ @ echo @ echo $[$RANDOM%$file_num+1] `luck= `head-n $line $file | & echo $luck > > / shell04/luck_ Num. Txtautomotive automotive hammer binram bashfor (iTun1) I user_pass.file# Loop create 5 user for ((iDev. X I > user_pass.file# Loop create 5 user while read user passdouseradd $userecho $pass | passwd-- stdin $userdone

< user_pass.filepwgen工具产生随机密码:[root@server shell04]# pwgen -cn1 12Meep5ob1aesa[root@server shell04]# echo user0{1..3}:$(pwgen -cn1 12)user01:Bahqu9haipho user02:Feiphoh7moo4 user03:eilahj5eth3R[root@server shell04]# echo user0{1..3}:$(pwgen -cn1 12)|tr ' ' '\n'user01:eiwaShuZo5hiuser02:eiDeih7aim9kuser03:aeBahwien8co二、嵌套循环 关键字:大圈套小圈 :clock3:时钟:分针与秒针,秒针转⼀圈(60格),分针转1格。循环嵌套就是外层循环⼀次,内层循环⼀轮。 一个==循环体==内又包含另一个完整的循环结构,称为循环的嵌套。每次外部循环都会==触发==内部循环,直至内部循环完成,才接着执行下一次的外部循环。for循环、while循环和until循环可以相互嵌套。1. 应用案例1)打印指定图案1121231234123455545435432543212)落地实现1X轴:for ((i=1;i=$y;x--)) do echo -n $x doneechodone#!/bin/bashy=5while [ $y -ge 1 ]do for ((x=5;x>

) do echo-n $x doneecholet y Mustang color pictures (($y > 5)) do Xerox while (($x ~ / ip_up.txt / usr/bin/expect / dev/nulltest $?-eq 0 & & echo "Public key successfully pushed complete" 2. Actual combat case 2

Write a script to count the difference of web services = = connection status = = number

#! / bin/bash#count_http_80_state# counts the number of states declare-An array1states= `ss-ant | grep 80 | cut-d'-f1`for I in $statesdo let array1 [$I] + + done# prints out for j in ${! array1 [@]} do echo $done by traversing the indexes and elements in the array

The above is the Shell production of random numbers and nested loops of the method introduction, the detailed use of the need for you to use their own hands in order to understand. If you want to know more about it, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report