[Shell] Shell Learning notes for

Source: Internet
Author: User

There are a lot of uses for the For loop in the shell, and I've been trying to summarize what I saw on the web today about the for loop usage.
Knot, feel very comprehensive, so turn around research and research, hey ...
1, for ((i=1;i<=10;i++));d o echo $ (expr $i \* 4);d One
2. Common in Shell is for I in $ (seq 10)
3, for i in ' ls '
4, for I in ${arr[@]}
5, for I in $*; Do
6, for File in/proc/sys/net/ipv4/conf/*/accept_redirects; Do
7, for I in F1 F2 F3;d o
8, for I in *.txt
9, for I in $ (LS *.txt)
The for-in statement is used in combination with "' and $ (), with the flaw of" or $ () that would be a line of lines, actually combining a string
Array
============-_-==============for num in $ (SEQ 1 100)
10, list= "Rootfs usr data data2″
For d in $LIST; Do
Use the for in statement to automatically traverse the character string by space, traversing multiple directories
11, for I in {1..10}
12, for I in Stringchar {1..10}
13. awk ' Begin{for (i=1; i<=10; i++) Print i} '
Note: The For loop notation in awk is the same as the C language
===============================================================
01.#/bin/bash
02.# Author: Zhou Haihan
03.# date:2010.3.25
04.# Blog.csdn.net/ablo_zhou
05.arr= ("A" "B" "C")
06.echo "Arr is (${arr[@]})"
07.echo "item in array:"
08.for i in ${arr[@]}
09.do
echo "$i"
11.done
12.echo "parameter, \$* represents all parameters entered by the script:"
13.for i in $*; Do
14.echo $i
15.done
16.echo
17.echo ' processing file/proc/sys/net/ipv4/conf/*/accept_redirects: '
18.for File in/proc/sys/net/ipv4/conf/*/accept_redirects; Do
19.echo $File
20.done
21.echo "Specify circular content directly"
22.for i in F1 f2 F3;d o
23.echo $i
24.done
25.echo
26.echo "C syntax for loop:"
27.for ((i=0; i<10; i++)); Do
28.echo $i
29.done
———————————————————————————–
———————-
Shell in for loop usage
Shell syntax Good trouble, a loop to get a while, find a few different ways to achieve output 1-100 can be 3
The number of divisible
1. using (())
#!/bin/bash
Clear
For ((i=1;i<100;i++))
For
Do
if ((i%3==0))
Then
Echo $i
Continue
Fi
Done
2. Use ' SEQ 100 '
#!/bin/bash
Clear
For i in ' SEQ 100 '
Do
if ((i%3==0))
Then
Echo $i
Continue
Fi
Done
3. Using the While
#!/bin/bash
Clear
I=1
while (($i <100))
Do
if (($i%3==0))
Then
Echo $i
Fi
i=$ (($i + 1))
Done
———————————————————————————–
———————
When the shell uses the for loop to make the number increment, it finds a problem, and lists several methods for the shell for Loop:
1.
For i in ' seq 1 1000000 ';d o
Echo $i
Done
Increment with seq 1 10000000, before using this method did not encounter problems, because the previous I was useless to millions
(1000000) because the project needs me this number is far greater than million, found to convert the SEQ value to 1000000 when converted to 1e+06, the root
Can not be used as a number of other operations, or the $i effective, correct access, and then seek other methods to solve, as follows
2.
For ((i=1;i<10000000;i++));d o
Echo $i
Done
3.
I=1
while (($i <10000000));d o
Echo $i
i= ' expr $i + 1 '
Done
Because this method calls expr so the speed will be slower than the 1th, 2nd, but can be slightly improved, the i= ' expr $i + 1 ' change
For i=$ ($i + 1) It is possible to increase the speed slightly, but depends on whether the shell environment supports
4.
For I in {1..10000000;do
Echo $i
Done
In fact, the choice of which method specific or to be supported by the corresponding shell environment, to achieve the desired effect, and then consider the speed of
Problem.

[Shell] Shell Learning notes for

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.