shell中的for語句疑惑
以下是man里的說明,有說到如果 for in 的 in 語句省略的話,就對位置參數進行循環。所以你的理解是對的:
for name [ [ in [ word 。 ] ] ; ] do list ; done
The list of words following in is expanded, generating a list of items. The variable name is set to
each element of this list in turn, and list is executed each time. If the in word is omitted, the for
command executes list once for each positional parameter that is set (see PARAMETERS below). The return
status is the exit status of the last command that executes. If the expansion of the items following in
results in an empty list, no commands are executed, and the return status is
用shell的for語句結構和while語句結構編寫程序-搜狗問問
1、以下是shell腳本代碼:
#!/bin/bash
for ((j=1;j<10;j++))
do
for ((i=1;i<j;i++))
do
echo -en "\t"
done
for ((k=j;k<10;k++))
do
echo -en "$((k*j))\t"
done
echo ""
done
2、執行結果見下圖
請教大神shell的for in語句中怎么使用多個變量
下圖就是你要達到的目的吧,*為10行內容,*為10行內容,C目錄有10個文件,*為實現的shell腳本。
上圖就是你要達到的目的吧,*為10行內容,*為10行內容,C目錄有10個文件,*為實現的shell腳本。
或者如下圖:
#!/bin/bash
a=(`cat *`)
b=(`cat *`)
c=(`ls C`)
for (( i = 0; i do
echo ${a[$i]} ${b[$i]} ${c[$i]}
done
shell里的for循環怎么寫
for循環的運作方式,是講串行的元素意義取出,依序放入指定的變量中,然后重復執行含括的命令區域(在do和done 之間),直到所有元素取盡為止。
其中,串行是一些字符串的組合,彼此用$IFS所定義的分隔符(如空格符)隔開,這些字符串稱為字段。for的語法結構如下:1234for 變量 in 串行do 執行命令done說明: 行1,講串行中的字段迭代放入變量中 行2-4,接著將重復執行do和done之間的命令區域,直到串行中每一個字段軍處理過為止。
流程圖:用例1 用for循環在家目錄下創建aaa1-aaa10,然后在aaa1-aaa10創建bbb1-bbb10的目錄123456789101112#!/bin/bashfor k in $( seq 1 10 )do mkdir /home/kuangl/aaa${k} cd /home/kuangl/aaa${k} for l in $( seq 1 10 ) do mkdir bbb${l} cd /home/kuangl/aaa${k} done cd ..done說明: 行2,seq 用于產生從某個數到另外一個數之間的所有整數。 行4,在家目錄下創建文件夾。
行6,在使用一個for循環創建文件夾測試結果:12345678910111213141516171819202122232425[root@test01 kuangl]# lltotal 80drwxr-xr-x 12 root root 4096 Jul 20 01:23 aaa1drwxr-xr-x 12 root root 4096 Jul 20 01:23 aaa10drwxr-xr-x 12 root root 4096 Jul 20 01:23 aaa2drwxr-xr-x 12 root root 4096 Jul 20 01:23 aaa3drwxr-xr-x 12 root root 4096 Jul 20 01:23 aaa4drwxr-xr-x 12 root root 4096 Jul 20 01:23 aaa5drwxr-xr-x 12 root root 4096 Jul 20 01:23 aaa6drwxr-xr-x 12 root root 4096 Jul 20 01:23 aaa7drwxr-xr-x 12 root root 4096 Jul 20 01:23 aaa8drwxr-xr-x 12 root root 4096 Jul 20 01:23 aaa9[root@test01 kuangl]# cd aaa1[root@test01 aaa1]# lltotal 40drwxr-xr-x 2 root root 4096 Jul 20 01:23 bbb1drwxr-xr-x 2 root root 4096 Jul 20 01:23 bbb10drwxr-xr-x 2 root root 4096 Jul 20 01:23 bbb2drwxr-xr-x 2 root root 4096 Jul 20 01:23 bbb3drwxr-xr-x 2 root root 4096 Jul 20 01:23 bbb4drwxr-xr-x 2 root root 4096 Jul 20 01:23 bbb5drwxr-xr-x 2 root root 4096 Jul 20 01:23 bbb6drwxr-xr-x 2 root root 4096 Jul 20 01:23 bbb7drwxr-xr-x 2 root root 4096 Jul 20 01:23 bbb8drwxr-xr-x 2 root root 4096 Jul 20 01:23 bbb91用例2 列出var目錄下各子目錄占用磁盤空間的大小。1234567#!/bin/bashDIR="/var"cd $DIRfor k in $(ls $DIR)do [ -d $k ] && du -sh $kdone說明: 行4,對/var目錄中每一個文件,進行for循環處理。
行6,如果/var下的文件是目錄,則使用du -sh計算該目錄占用磁盤空間的大小。測試結果:12345678910111213141516171819202122[root@test01 scripts]# ./*276M cache4.0K cvs84K db8.0K empty4.0K ftp4.0K games4.0K gdm21G lib4.0K local16K lock7.4G log4.0K logs0 mail4.0K nis4.0K opt4.0K preserve336K run3.7G spool212K tmp4.1G www4.0K yp。
shell語言 循環語句怎么寫
echo –n “input:”
read user
if 多條指令,這些命令之間相當于“and”(與)
grep $user /etc/passwd >/tmp/null
who -u | grep $user then 上邊的指令都執行成功,返回值$?為0,0為真,運行then
echo "$user has logged"
else 指令執行失敗,$?為1,運行else
echo "$user has not logged"
fi
linux中使用for語句創建命令行上所有整數只和的shell
[root@localhost ~]# cat e1#!/bin/basha=$*sum=0for i in $ado sum=`expr $sum + $i`doneecho $sum[root@localhost ~]# sh -x e1 123 45 67+ a='123 45 67'+ sum=0+ for i in '$a'++ expr 0 + 123+ sum=123+ for i in '$a'++ expr 123 + 45+ sum=168+ for i in '$a'++ expr 168 + 67+ sum=235+ echo 235235。
轉載請注明出處華閱文章網 » shell的for語句