Some easy-to-confuse concepts about shell scripting

Source: Internet
Author: User

    1. [email protected] and $* the difference

[Email protected]~] $vim my_fun.sh

#!/bin/bash

# [email protected] and $*

My_fun () {

echo "$#"

}


Echo ' The number of parameter in ' [email protected] ' is ' $ (my_fun "[email protected]")

Echo ' The number of parameter in [email protected] is ' $ (my_fun [email protected])

Echo ' The number of parameter in ' $* ' is ' $ (my_fun "$*")

Echo ' The number of parameter in $* ' $ (my_fun $*)

~


[[Email protected] ~]$ sh a.sh p1 "P2 P3" P4

The number of parameter in "[E-mail protected]" is 3

The number of parameter in [e-mail protected] is 4

The number of parameter in "$*" is 1

The number of parameter in $* is 4

Do you see the difference? key [email protected] be a little more reliable


2. About > and <

[email protected] ~]$ cat <file >file.bak

[email protected] ~]$ cat File;cat File.bak

This is a test

This is a test

[email protected] ~]$ cat <file >file

[[email protected] ~]$ cat file

[[email protected] ~]$ =====> This line of output is empty, the file has no content

Why can't I see the contents of the file in the last cat?

This is because the file is really an empty file at this point. Why???

Because in IO redirection, the standard output is prepared before it is read from the standard input to the standard output. is not a bit around.

It's like passing the baton in a relay race; you don't see the guy with the stick, you're definitely not going to send the baton out.

So, in the cat <file >file command, > will empty the file before it executes > files, and the result is empty.


3. How variables are evaluated in a for loop

A. Taking values from a set of strings

For Var in one, three four five

Do

Echo ****************************

Echo ' $var is ' $var

Done


B. Taking a value from a position variable

For Var;do

Do

Echo '-----------------------------'

Echo ' $var is ' $var

Done

C. Taking values from a cumulative change in format

#!/bin/bash

For ((var=1;var<=10;var++))

Do

Echo------------------------

Echo ' $var is ' $var

Done

D. Taking values from the command substitution

#!/bin/bash

For Var in $ (cat file.txt)

Do

Echo------------------------

Echo ' $var is ' $var

Done


This article is from the Linux OPS architect blog, so be sure to keep this source http://sharkyun.blog.51cto.com/10455795/1792976

Some easy-to-confuse concepts about shell scripting

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.