Method for shell for loop, loop variable value to pay other shell scripts

Time:2019-6-27

This article focuses on how to write for loops in the shell, and takes loop variables as parameters of the next shell script.

Shell for loop:

# The first is similar to C and Java.
for ((i=1; i<=100; i ++))
do
  echo $i  
done
# The second way of writing in application
for i in {1..100} 
do 
  echo $i 
done 
# The third way of writing seq
for i in `seq 1 100` 
do 
  echo $i 
done

Assign loop variables to the next script:

When running shell scripts, there are three ways to call external scripts: exec (exec script. sh), source (source script. sh), fork (. / script. sh).

1、exec(exec /home/script.sh):

Using exec to call the script, the executed script inherits the environment variables of the current shell. But in fact, exec creates a new process, which takes up the process resources of the main shell and replaces the script content, inherits the ID number of the original main shell, that is, the remaining content of the original main shell will not be executed.

2、source(source /home/script.sh)

Using source or “…” to call external scripts will not generate new processes, inherit the current shell environment variables, and after the script is run, the environment variables and declared variables it has will be retained by the current shell, similar to copying the content of the calling script to execute directly. After execution, the original main shell continues to run.

3、fork(/home/script.sh)

Running the script directly will take the current shell as the parent process, generate new processes, and inherit the environment variables and declaration variables of the main script. After execution, the main script does not retain its environment variables and declare variables.

# Main. sh Subject
#!/bin/sh
a=main

echo "a is $a"
echo "PID for parent before 2.sh:$$"
case $1 in
 exec)
  echo "using exec"
  exec ./2.sh ;;
 *)
  echo "using sourcing"
  source ./2.sh ;;
esac

echo "PID FOR parent after 2.sh :$$"

echo "now m"

#!2.sh
#!/bin/sh
echo "PID FOR 2.SH:$$"

echo "2.sh get a from main.sh is $a"

a=2.sh
export a
b=3.sh

echo "now 2.sh a is $a"

Implementation results:

a is main
PID for parent before 2.sh:1162
using sourcing
PID FOR 2.SH:1162
2. sh get a from main. SH is main `Write code slices here`
now 2.sh a is 2.sh
PID FOR parent after 2.sh :1162
now m

Through the for loop, the loop variable is assigned and executed as a 2.sh variable.

You! Main principal function
#!/bin/sh
a=0
for ((i=1; i<=10; i ++))
do
    a=$i
    echo "a is $a"
    echo "PID for parent before 2.sh:$$" 
        echo "using sourcing"
        source ./2.sh
     echo "PID FOR parent after 2.sh :$$"
    echo "now a is $a" 
done

Output results:


a is 1
PID for parent before 2.sh:1339
using sourcing
PID FOR 2.SH:1339
2.sh get a from main.sh is 1
now 2.sh a is 2.sh
PID FOR parent after 2.sh :1339
now a is 2.sh
a is 2
PID for parent before 2.sh:1339
using sourcing
PID FOR 2.SH:1339
2.sh get a from main.sh is 2
now 2.sh a is 2.sh
PID FOR parent after 2.sh :1339
now a is 2.sh
a is 3
PID for parent before 2.sh:1339
using sourcing
PID FOR 2.SH:1339
2.sh get a from main.sh is 3
now 2.sh a is 2.sh
PID FOR parent after 2.sh :1339
now a is 2.sh
a is 4
PID for parent before 2.sh:1339
using sourcing
PID FOR 2.SH:1339
2.sh get a from main.sh is 4
now 2.sh a is 2.sh
PID FOR parent after 2.sh :1339
now a is 2.sh
a is 5
PID for parent before 2.sh:1339
using sourcing
PID FOR 2.SH:1339
2.sh get a from main.sh is 5
now 2.sh a is 2.sh
PID FOR parent after 2.sh :1339
now a is 2.sh
a is 6
PID for parent before 2.sh:1339
using sourcing
PID FOR 2.SH:1339
2.sh get a from main.sh is 6
now 2.sh a is 2.sh
PID FOR parent after 2.sh :1339
now a is 2.sh
a is 7
PID for parent before 2.sh:1339
using sourcing
PID FOR 2.SH:1339
2.sh get a from main.sh is 7
now 2.sh a is 2.sh
PID FOR parent after 2.sh :1339
now a is 2.sh
a is 8
PID for parent before 2.sh:1339
using sourcing
PID FOR 2.SH:1339
2.sh get a from main.sh is 8
now 2.sh a is 2.sh
PID FOR parent after 2.sh :1339
now a is 2.sh
a is 9
PID for parent before 2.sh:1339
using sourcing
PID FOR 2.SH:1339
2.sh get a from main.sh is 9
now 2.sh a is 2.sh
PID FOR parent after 2.sh :1339
now a is 2.sh
a is 10
PID for parent before 2.sh:1339
using sourcing
PID FOR 2.SH:1339
2.sh get a from main.sh is 10
now 2.sh a is 2.sh
PID FOR parent after 2.sh :1339
now a is 2.sh

The above shell for loop, loop variable value paid to other shell scripts is the way the editor shares all the content to you, I hope to give you a reference, and I hope you will support developpaer more.

Recommended Today

JQuery’s promise

Promise is an asynchronous programming specification, transliteration to understand –“Prometheus” Prometheus is a hero in Greek mythology, whose name means “prophet”. Promise was originally named “futures”. It can be seen that promise does not mean literally “oath”, “promise”, etc., but “prophet” and “future”. Promise is to solve the most headache in asynchronous programmingCallback nesting region […]