Linux for-loop
We can print from 1 to 10 using the following script:
#bin/bash # loop.sh for n in 1 2 3 4 5 6 7 8 9 10 do echo "$n" done
The output looks like this:
$ ./loop.sh 1 2 3 4 5 6 7 8 9 10
We can switch the for with more compact form:
for n in {1..10}
We can setup step value using {start..end..inc}:
#bin/bash # step.sh for n in {1..10..2} do echo "$n" done
Output:
$ ./step.sh 1 3 5 7 9
But actually, the sep size is not supported for the Bash version < 4. So, the code can be modified like this:
if [ ${BASH_VERSINFO} -ge 4 ] then for n in {1..10..2} do echo "$n" done else echo "step : not supported in bash ${BASH_VERSION}" fi
Or:
if [ ${version:=${BASH_VERSINFO}} -ge 4 ] then for n in {1..10..2} do echo "$n" done else echo "step : not supported in bash $version" fi
To make it work regardless of the Bash version:
if [ ${BASH_VERSINFO} -ge 4 ] then for n in {1..10..2} do echo "$n" done else echo "step : not supported in bash ${BASH_VERSION}" echo "using seq" for n in $(seq 1 2 10) do echo "$n" done fi
Or:
echo "myArray" if [ ${BASH_VERSINFO} -ge 4 ] then myArray=${1..10..2} else myArray=$(seq 1 2 10) fi for n in $myArray do echo $n done
This type of for loop share a common syntax with the C programming language. It is characterized by a three-parameter loop control expression; consisting of an initializer (EXP1), a loop-test or condition (EXP2), and a counting expression (EXP3):
for (( EXP1; EXP2; EXP3 ))
Here is our script:
#bin/bash # C_style.sh for ((n=1; n<=10; n++)) do echo "$n" done
With the same output:
$ ./C_style.sh 1 2 3 4 5 6 7 8 9 10
If we do not want to put no new-line, we run this:
#bin/bash # no-newline.sh for ((a=1; a<=10; a++)) do echo -n "$a " done echo
Output should look like this:
$ ./no-newline.sh 1 2 3 4 5 6 7 8 9 10
Or we can get the same output by doing the last line differently (instead of putting echo at the end of the loop) :
n=10 for ((a=1; a<n; a++)) do if [ $a -eq $((n-1)) ] then echo $a else echo -n "$a " fi done
This time, let's add from 1 to 10:
#bin/bash # count10.sh sum=0 for ((i=1; i<=10; i++)) do sum=$((sum+i)) done echo "sum=$sum"
The output:
$ ./count10.sh sum=55
We can do infinite loop:
#bin/bash # inf.sh count=0 for ((;;)) do count=$((count+1)) echo -n "$count " done echo
Output:
$ ./inf.sh 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
We can use the for-loop to check the file in a folder:
#!/bin/bash # pass.sh lines=1 for file in /etc/* do if [ "${file}" == "/etc/passwd" ] then echo "passwd is the ${lines}-th file listed in /etc" break fi lines=$((lines+1)) done
Output:
$ ./pass.sh passwd is the 172-th file listed in /etc
Problem : Given N integers, compute their average correct to three decimal places.
Input Format :
The first line contains an integer, N.
N lines follow, each containing a single integer.
Output Format :
Display the average of the N integers, rounded off to three decimal places.
Input Constraints :
1<=N<=500
#! /bin/bash if [ $# -lt 2 ]; then echo "Need 2 args at least" exit 1 fi if [ $1 -lt 1 -o $1 -gt 500 ]; then echo " 1 <= N <= 500" exit 1 fi index=0 sum=0 for i in $@ do if [ $index -ne 0 ]; then array[$index]=$i sum=$((sum+i)) fi index=$((index+1)) done echo "input=${array[@]}" echo "sum=$sum" echo "average=$((sum/$1))"
Output looks like this:
$ ./avg.sh 6 10 20 30 40 50 60 input=10 20 30 40 50 60 sum=210 average=35
Linux - system, cmds & shell
- Linux Tips - links, vmstats, rsync
- Linux Tips 2 - ctrl a, curl r, tail -f, umask
- Linux - bash I
- Linux - bash II
- Linux - Uncompressing 7z file
- Linux - sed I (substitution: sed 's///', sed -i)
- Linux - sed II (file spacing, numbering, text conversion and substitution)
- Linux - sed III (selective printing of certain lines, selective definition of certain lines)
- Linux - 7 File types : Regular, Directory, Block file, Character device file, Pipe file, Symbolic link file, and Socket file
- Linux shell programming - introduction
- Linux shell programming - variables and functions (readonly, unset, and functions)
- Linux shell programming - special shell variables
- Linux shell programming : arrays - three different ways of declaring arrays & looping with $*/$@
- Linux shell programming : operations on array
- Linux shell programming : variables & commands substitution
- Linux shell programming : metacharacters & quotes
- Linux shell programming : input/output redirection & here document
- Linux shell programming : loop control - for, while, break, and break n
- Linux shell programming : string
- Linux shell programming : for-loop
- Linux shell programming : if/elif/else/fi
- Linux shell programming : Test
- Managing User Account - useradd, usermod, and userdel
- Linux Secure Shell (SSH) I : key generation, private key and public key
- Linux Secure Shell (SSH) II : ssh-agent & scp
- Linux Secure Shell (SSH) III : SSH Tunnel as Proxy - Dynamic Port Forwarding (SOCKS Proxy)
- Linux Secure Shell (SSH) IV : Local port forwarding (outgoing ssh tunnel)
- Linux Secure Shell (SSH) V : Reverse SSH Tunnel (remote port forwarding / incoming ssh tunnel) /)
- Linux Processes and Signals
- Linux Drivers 1
- tcpdump
- Linux Debugging using gdb
- Embedded Systems Programming I - Introduction
- Embedded Systems Programming II - gcc ARM Toolchain and Simple Code on Ubuntu/Fedora
- LXC (Linux Container) Install and Run
- Linux IPTables
- Hadoop - 1. Setting up on Ubuntu for Single-Node Cluster
- Hadoop - 2. Runing on Ubuntu for Single-Node Cluster
- ownCloud 7 install
- Ubuntu 14.04 guest on Mac OSX host using VirtualBox I
- Ubuntu 14.04 guest on Mac OSX host using VirtualBox II
- Windows 8 guest on Mac OSX host using VirtualBox I
- Ubuntu Package Management System (apt-get vs dpkg)
- RPM Packaging
- How to Make a Self-Signed SSL Certificate
- Linux Q & A
- DevOps / Sys Admin questions
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization