Don't you consider the procedure when you run shell scripts?

Asked 2 years ago, Updated 2 years ago, 108 views

Chapter 7 in the book "This Is Linux" (by Woo Jae-nam) Learning Shell Script Programming Unit

I had doubts.

*set.sh*

 1. #!/bin/sh
 2. echo "today's date is $(date)."
 3. set $(date)
 4. echo "today's day is $4day."
 5. exit 0

*Results*
Today's date is May 11, 2021 (Tue) 20:35:04 KST.
Today's day is Tuesday.

If you look at the introduction of the Shell Script Unit in the book, it says this.

Since most of Linux is written in C, shell scripts are similar to C can be programmed in a way

So, is shell script programming procedure oriented?

In the example above, $(date) in row 2 determines its value in row 3.

That's why it should be an error in C, but it works normally in shell script.

Please explain how it works!!

shell-script linux

2022-09-20 16:27

1 Answers

In the above example of the question, $(date) in row 2 is determined by row 3.The interpretation of is invalid.

The code you posted was executed one line at a time from the top.

$(a) is to execute the command a and bring the result back to its place.

echo "today's date is $(date)."In , date is a Linux command, and the result is returned to the $(date) digit, and today's date is 2021. 05. 11. (Tue) 20:35:04 KST. is printed."

In other words, the result of date is 2021.0511.(Tue) 20:35:04 KST.

set means that you want to write the execution result behind you like a command line argument.

set $(date) will get 2021.0511.(Tue) 20:35:04 KST which is the result of the date, which means that we will use this string like a command line argument.

Therefore, after set $(date) is executed, 2021. becomes $1; 05. becomes $2; 11. is $3; (Tue) is $4, and is $:205> is $ST.

And $ 4 in the daeumjul day the (anger) day, output .


2022-09-20 16:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.