In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
We all know that ordinary bash commands can be followed by arbitrary parameters, so do our own scripts also support passing parameters? Of course the answer is yes.
Execute "vim test.sh" to create a new shell script. The script test.sh is as follows:
#! / bin/sh
Name=$1
Echo "the ${name} are great man!"
Assign executable permissions to the script of the newly created test.sh with the command "chmod + x test.sh"
Execute the command'. / test.sh "xiao wang" and you can see the result of your own script "the xiao wang are great man!"
In "name=$1", $1 is the location parameter provided by the system, $0 represents the name of the program (. / test.sh), [$1 $2.] Parameters that are passed starting at 1. In addition to location parameters, the linux system also provides built-in parameters, all of which are as follows:
$0-current script file name
A parameter passed to a script or function, where n is a number indicating the parameter, $1 represents the first parameter, and $2 represents the second parameter.
$#-the number of parameters passed to the script or function
$*-all parameters passed to a script or function, and when it is enclosed in double quotes (""), "$*" takes all parameters as a whole and outputs all parameters in the form of "$1 $2. $n"
$@-all parameters passed to a script or function, when enclosed in double quotation marks (""), are slightly different from $*, "$@" separates each parameter with "$1"$2"... Output all parameters in the form of "$n"
When $* and $@ are not contained in double quotes (""), they both use "$1" and "$2". Output all parameters in the form of "$n"
Let's write a Mini Program to practice $* and $@
$? the exit status of the previous command, or the return value of the function. If it exits normally, it returns 0, otherwise it is a non-zero value.
$$- the current shell process pid. For shell scripts, it is the process ID where these scripts are located
$!-pid of the previous command
Create a new test.sh chmod + x test.sh to execute. / test.sh "a"b"c"d" to observe the print result
#! / bin/bash
Echo "\ $* =" $*
Echo "\ $@ =" $@
Echo "print each param from\ $*"
For var in $*
Do
Echo $var
Done
Echo "print each param from\ $@"
For var in $@
Do
Echo $var
Done
Echo "print each param from\"\ $*\ ""
For var in "$*"
Do
Echo $var
Done
Echo "print each param from\"\ $@\ ""
For var in "$@"
Do
Echo $var
Done
Print the results:
$* = a b c d
$@ = a b c d
Print each param from $*
A
B
C
D
Print each param from $@
A
B
C
D
Print each param from "$*"
A b c d
Print each param from "$@"
A
B
C
D
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.