for循环

#!/bin/sh

for i in a b c d e f ; do
echo $i #把a~f的内容,逐个给$i
done


============================================

#!/bin/sh
WORD="a b c d e f g h i j l m n o p q r s t u v w x y z"

for i in $WORD ; do
echo $i
done


=============================================


修改副档名
如果您有许多的.txt档想要改名成.doc档,您不需要一个一个来。
#!/bin/sh

FILES=`ls /txt/*.txt`

for txt in $FILES ; do
doc=`echo $txt | sed "s/.txt/.doc/"`
mv $txt $doc
done

这样可以将*.txt档修改成*.doc档。



===========================================

meow
#!/bin/sh
# Filename : meow
for i ; do
cat $i
done

当您输入"meow file1 file2 ..."时,其作用就跟"cat file1 file2 ..."一样。



============================================


#!/bin/sh
# Filename : listbin

for i in /bin/* ; do
echo $i
done

当您输入"listbin"时,其作用就跟"ls /bin/*"一样。



=============================================


/etc/rc.d/rc
拿一个实际的例来说,Red Hat的/etc/rc.d/rc的启动程式中的一个片断。

for i in /etc/rc.d/rc$runlevel.d/S*; do
# Check if the script is there.
[ ! -f $i ] && continue

# Check if the subsystem is already up.
subsys=${i#/etc/rc.d/rc$runlevel.d/S??}
[ -f /var/lock/subsys/$subsys ] || \
[ -f /var/lock/subsys/${subsys}.init ] && continue

# Bring the subsystem up.
$i start
done


==============================================


 » 相关连接:
for what? while 與 until 差在哪 实现洗牌效果 什么是shell 去除文件中的连续空白行
取子串 数组 exec eval问题
设置/删除环境变量 echo的一些特殊用法 算术表述 > 與 < 差在哪?
[回复]
#!/bin/sh
#1)
for i in  "data""data/sql_config.php" "data/tpl_cache" "data/sql" "data/rss""template/user" "attachment" "attachment/temp" "attachment/s" "www""script/verycms"
do
   echo $i
   chmod 777 $i
done


#2)

Array=("data""data/sql_config.php" "data/tpl_cache" "data/sql" "data/rss""template/user" "attachment" "attachment/temp" "attachment/s" "www""script/verycms")
for i in  ${Array[*]}     #这里${Array[*]}表示所有数组myshowbaidu('%D4%AA%CB%D8',2,'元素');元素
do
   echo $i
   chmod 777 $i

 » 相关连接:
for what? while 與 until 差在哪 实现洗牌效果 什么是shell 去除文件中的连续空白行
取子串 数组 exec eval问题
设置/删除环境变量 echo的一些特殊用法 算术表述 > 與 < 差在哪?
[回复]
[cnscnor@cnslinux.com /www]# for i in $(ls www_sitemap*) ;  do  sed  -e  's/cnslinux.com/www.cnslinux.com/'  $i  >  bak_$i;  done

 » 相关连接:
for what? while 與 until 差在哪 实现洗牌效果 什么是shell 去除文件中的连续空白行
取子串 数组 exec eval问题
设置/删除环境变量 echo的一些特殊用法 算术表述 > 與 < 差在哪?
[回复]
9.1.1. 如何工作?

for 循环是3个shell循环结构中的第一个。这个循环允许指定一个值的列表。列表中的命令为每个在列表中的值执行。

这个循环的语法结构是这样的:

for NAME [in LIST ]; do COMMANDS; done

如果 [in LIST] 不存在的话,就使用 in $@ 替换且 for 对每个位置参数执行一次 COMMANDS (参见 第 3.2.5 节 “特殊参数” 和 第 7.2.1.2 节 “检查命令行参数”).

返回值为最后执行命令的退出状态。如果由于没有 LIST 中没有任何项目就不执行命令,返回值是零。

NAME 可以使任何变量的名字,虽然 i 的使用非常普遍。 LIST 可以是任何字的列表,字符串和数字,命令生成。执行的 COMMANDS 也可以是任何操作系统命令,脚本,程序或者shell语句。第一次执行循环的时候,NAME 设置为LIST 中的第一个项目。第二次,它的值设置成列表中的第二个项目,以此类推。当 NAME 得到了在 LIST 中所有的值之后循环中止。
9.1.2. 例子
9.1.2.1. 为指定的LIST项目使用命令替换

第一个是命令行例子,证明 for 循环的使用来为每个 .xml 文件做备份。执行了这个命令之后,在你的源代码上工作就变得很安全:

[carol@octarine ~/articles] ls *.xml
file1.xml  file2.xml  file3.xml

[carol@octarine ~/articles] ls *.xml > list

[carol@octarine ~/articles] for i in `cat list`; do cp "$i" "$i".bak ; done

[carol@octarine ~/articles] ls *.xml*
file1.xml  file1.xml.bak  file2.xml  file2.xml.bak  file3.xml  file3.xml.bak

这个列出了在 /sbin 中的正好是纯文本的文件,这是可能的脚本:

for i in `ls /sbin`; do file /sbin/$i | grep ASCII; done

9.1.2.2. 使用一个变量的内容来指定LIST项目

以下是一个特别的应用脚本来转换适应一个特定的schema的HTML文件到PHP文件,转化通过取出最初的25行和最后的21行,用PHP标签替换来提供页眉和页脚来完成的:

[carol@octarine ~/html] cat html2php.sh
#!/bin/bash
# specific conversion script for my html files to php
LIST="$(ls *.html)"
for i in "$LIST"; do
    NEWNAME=$(ls "$i" | sed -e 's/html/php/')
    cat beginfile > "$NEWNAME"
    cat "$i" | sed -e '1,25d' | tac | sed -e '1,21d'| tac >> "$NEWNAME"
    cat endfile >> "$NEWNAME"
done

既然这里我们没有做一个行计数,就没有办法知道开始删除的行直到到达的行的行数。这个问题可以用 tac, 它将在文件中保留行

 » 相关连接:
for what? while 與 until 差在哪 实现洗牌效果 什么是shell 去除文件中的连续空白行
取子串 数组 exec eval问题
设置/删除环境变量 echo的一些特殊用法 算术表述 > 與 < 差在哪?
[回复]
bash中的for循环有几种方式:

1.for name [ in word ] ; do
        list ;
  done

2.for (( expr1 ; expr2 ; expr3 )) ;  do
        list ;
    done//注意有2层括号

其中list简单说就是一串由操作符(operator ;、&、&&、||)分隔开的管道(pipeline)序列,详情参看man bash
各给出一个简单例子:
1. in
for filename in `ls`
do
  cat $filename
done

2. ++
for((i=0; i<10; i++))
do
  echo $i
done

3.--
for (( i = 300; i >= 1 ;i-- ))
do
  #echo -ne  "`date '+%T'` "
  echo -ne "$i "
  sleep 1
done

5. 每次减5
for (( i = 300; i >= 1 ;i-=5 ))
do
  echo -ne "$i "

  i=$(expr \( $i - 4 \));
done

5. 每次减5
for (( i = 300; i >= 1 ;i-=5 ))
do
  echo -ne "$i "
done

5. 每次加5
for (( i = 300; i >= 1 ;i+=5 ))
do
  echo -ne "$i "
done

 » 相关连接:
for what? while 與 until 差在哪 实现洗牌效果 什么是shell 去除文件中的连续空白行
取子串 数组 exec eval问题
设置/删除环境变量 echo的一些特殊用法 算术表述 > 與 < 差在哪?
[回复]
依次创建7个目录
[root@fileserver ~]# for ((i=1;i<=7;i++));  do mkdir /mysql/data${i};  done

 » 相关连接:
for what? while 與 until 差在哪 实现洗牌效果 什么是shell 去除文件中的连续空白行
取子串 数组 exec eval问题
设置/删除环境变量 echo的一些特殊用法 算术表述 > 與 < 差在哪?
 » 本栏目最新帖:

Powered by PHPWind v6.0 Code © 2003-08