Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 622
  • Last Modified:

Need simple example of loop in unix shell script

Need simple example of loop in unix shell script
0
digs developer
Asked:
digs developer
1 Solution
 
Brian UtterbackPrinciple Software EngineerCommented:
Which shell? What kind of loop?

Here are some examples:

#!/bin/sh
for i in 1 2 3 4 5
do
  echo "Looping ... number $i"
done

#!/bin/sh
for i in hello 1 * 2 goodbye
do
  echo "Looping ... i is set to $i"
done

#!/bin/sh
INPUT_STRING=hello
while [ "$INPUT_STRING" != "bye" ]
do
  echo "Please type something in (bye to quit)"
  read INPUT_STRING
  echo "You typed: $INPUT_STRING"
done
0
 
savoneCommented:
While loop....

i=1
while [ "$i" -lt "100" ]; do
      echo $i
      let "i=i+1"
      sleep 1
done
0
 
TintinCommented:
Loop to read a file

while read line
do
   echo "$line"
done <filename

Open in new window

0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
# loop over each line in a file called file.txt
cat file.txt | while read line
do
  echo "Processing line: ${line}"
  # do something
done;

# endless loop
while [ 1 ]
do
  echo "Endless loop"
  sleep 10
  echo "Press CTRL-C to quit"
  sleep 2
done;
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now