Linux Shell   for循环结构

循环结构

     

     1:循环开始条件

     2:循环操作

     3:循环终止的条件

shell语言

    

    for,while,util

  

for循环

语法:

(1)

for 变量 in 取值列表;do

    statement

    statement

done

(2)

for 变量 in 取值列表

do

    statement

    statement

done

上面两个用法的效果是一样的。

取值列表:

    数字

        10 20 30

        使用seq命令生成数字的序列

            seq 10

            seq 3 10

            seq 1 2 10

    

    字符

        aa bb  cc

    

    文件

        

示例

seq可以快速去值,奇数等

[root@wei for]# seq 5
1
2
3
4
5
[root@wei for]# seq 2 6
2
3
4
5
6

示例:1--100的累加和

#!/bin/bash
#
sum=0
for i in `seq 1 100`
do
    let sum=$sum+$i
done
echo $sum  [root@wei for]# bash 1.sh 
5050

示例:1--100的奇数累加和

#!/bin/bash

sum=0

for i in `seq 100`
do
    let ys=$i%2
    if [ $ys -ne 0 ];then
        let sum=$sum+$i
    fi done echo $sum [root@wei for]# bash 2.sh 
2500

创建10个用户,初始密码为:root,登陆重新修改密码

#!/bin/bash
# for i in `seq 10`
do
    if ! id user$i &> /dev/null ; then
        useradd user$i
        echo "root" | passwd --stdin user$i &> /dev/null
        passwd -e user$i &> /dev/null
        echo "用户user$i创建完成,初始密码为:root"
    else
        echo "用户user$i已经存在"
    fi
done

以字符作为取值类表

#!/bin/bash
# for name in a d c d ;do
    useradd $name
    echo "$name create finishe"
done

以文件作为取值列表

        `cat file`

        
编写脚本,读取文本

#!/bin/bash 
#
for i in `cat /shell/for/1.txt`;do
    echo "line:$i"
done
[root@wei for]# ./wen.sh 
line:nangong
line:chengfneg

Linux shell for循环结构的更多相关文章

  1. shell脚本--循环结构

    shell的循环结构有while和for两种 for循环 #!/bin/bash #文件名:test.sh i=4 for i in 2 4 6 8 10 do echo $i done echo $ ...

  2. linux shell脚本使用结构化命令

    内容: 一.if-then命令 二.if-then-else命令 三.test命令 四.case命令 1.if-then结构化命令中最基本的类型,其格式如下: if command then comm ...

  3. linux shell脚本使用结构化命令(2)

    一.for命令 二.while命令 三.until命令 1.for命令基本格式 for var in list do commands done oracle@suse:~/testshell> ...

  4. Linux Shell编程 循环语法

    for循环 for 循环是固定循环,也就是在循环时已经知道需要进行几次循环.有时也把 for 循环称为计数循环.语法: for 变量 in 值1 值2 值3… do 程序 done 在这种语法中,fo ...

  5. Linux shell while循环语句

    for :明确循环次数 while :不确定循环换次数 while循环 (1) while CONDITION:do       statement       statement       < ...

  6. Linux shell 中断循环语句

    无限循环: 循环有限的生命,他们跳出来,一旦条件是 false 还是 false 取决于循环. 由于所需的条件是不符合一个循环可能永远持续下去.永远不会终止执行一个循环执行无限次数.出于这个原因,这样 ...

  7. linux shell for循环使用命令中读取到的值实例

    #!/bin/bash file="states" for state in `cat $file` do echo "Visit beautiful $state&qu ...

  8. linux shell脚本常用语句

    linux shell 指令 诸如-d, -f, -e之类的判断表达式: 文件比较运算符-e filename  如果 filename存在,则为真  [ -e /var/log/syslog ]-d ...

  9. Bash For Loop Examples for Your Linux Shell Scripting--ref

    There are two types of bash for loops available. One using the “in” keyword with list of values, ano ...

随机推荐

  1. spring框架的定时任务cronExpression表达式详解

    附:cronExpression表达式解释: 0 0 12 * * ?---------------在每天中午12:00触发 0 15 10 ? * *---------------每天上午10:15 ...

  2. Hello 2019 F 莫比乌斯反演 + bitset

    https://codeforces.com/contest/1097/problem/F 题意 有n个多重集,q次询问,4种询问 1. 将第x个多重集置为v 2. 将第y和z多重集进行并操作,并赋值 ...

  3. 【51Nod 1769】Clarke and math2

    [51Nod 1769]Clarke and math2 题面 51Nod 题解 对于一个数论函数\(f\),\(\sum_{d|n}f(d)=(f\times 1)(n)\). 其实题目就是要求\( ...

  4. 【转】hibernate对象三种状态

    hibernate里对象有三种状态: 1,Transient 瞬时 :对象刚new出来,还没设id,设了其他值. 2,Persistent 持久:调用了save().saveOrUpdate(),就变 ...

  5. Express服务器开发

    作者 | Jeskson 来源 | 达达前端小酒馆 Express服务器开发 创建Express应用程序,Express路由,pug视图模板的使用 Express简介: 让我们来创建Express应用 ...

  6. Redis数据结构及常用命令(草稿)

    通用命令 数据类型 string 字符 list 列表 set 集合 zset 有序集合 hash 散列(字典中的字典) bitmap 位图 hyperloglog

  7. Elasticsearch由浅入深(九)搜索引擎:query DSL、filter与query、query搜索实战

    search api的基本语法 语法概要: GET /_search {} GET /index1,index2/type1,type2/_search {} GET /_search { , } h ...

  8. Java Scala获取所有注解的类信息

    要想获取使用指定注解的类信息,可借助工具: org.reflections.Reflections 此工具将Java反射进行了高级封装,Reflections 通过扫描 classpath,索引元数据 ...

  9. windows上MongoDB远程访问配置

    今天用另一台机器上的MongoDB,但是使用本地连接时,没问题 换成IP地址时,出现 解决的方法,修改配置文件 systemLog: destination: file path: d:/Mongod ...

  10. docker 安装 apollo

    apollo作为携程开源的配置中心,很多大厂在使用,在此记录下安装历程 服务器环境: 安装mysql 1.拉取镜像 docker pull idoop/docker-apollo 2.新建3个数据库, ...