Shell programming shell script default variables ($, $ ...)

Source: Internet
Author: User
Tags syslog

Default variable for Shell script ($ ...)

We know that directives can have options and parameters, such as Ls-la to see all properties and permissions that contain hidden files. So can shell script have parameters behind the script file name? It's fun! For example, if you want to restart the system login file functionality, you can do this:

[Email protected] ~]# File/etc/init.d/syslog

/etc/init.d/syslog:bourne-again Shell Script Text executable

# After using file to query, the system tells the file is a bash executable script Oh!

[Email protected] ~]#/etc/init.d/syslog restart

Restart is the meaning of restarting, the above instruction can "restart/etc/init.d/syslog this program" meaning! Well! So what if you add stop after/etc/init.d/syslog? That's right! You can close the service directly! So amazing? Oh, yes! If you want to give some variables to perform different tasks depending on the execution of the program, this chapter starts with the Read feature! But the problem with the Read feature is that you have to manually enter some judgment on the keyboard. If the parameter is followed by the command, then a single instruction can be processed without having to manually enter some variable behavior again! It would be easier and more convenient to execute the instructions!

How does script achieve this function? In fact, script for the parameter has been set some variable name! Corresponds to the following:

/path/to/scriptname opt1 opt2 OPT3 opt4

$ $4

Is that clear enough for you? Execute the script file named "$" variable, the first parameter is $ ~ ~ So, as long as we use in script, we can easily execute some command function immediately! In addition to the variables of these numbers, we have a few more special variables that can be used within a script to invoke these parameters Oh!

$#: On behalf of the following parameters "number", the above table as an example shown here as "4";

[Email protected]: for "$" "" $ "" $ "" $ "" $4 "meaning, each variable is independent (enclosed in double quotation marks);

$*: Represents "$1c$2c$3c$4" ", where C is the separator character and the default is the SPACEBAR, so in this example," "$ $ $4" means.

That [email protected] and $* are basically different! However, the general use of the situation can be directly memory [email protected] can!

All right, let's do an example. Suppose I'm going to execute a script that can carry arguments, and after executing it, the screen displays the following data:

What is the file name of the program?

How many parameters are there?

If the number of parameters is less than 2, it tells the user that the number of parameters is too low

What are all the parameters?

What is the first parameter?

What is the second parameter?

[Email protected] scripts]# VI sh07.sh

#!/bin/bash

# program:

# program shows the script name, parameters ...

# History:

echo "The script name is ==>"

echo "Total parameter number is ==> $#"

["$#"-lt 2] && echo "The number of parameter is less than 2.  Stop here. " && Exit 0

echo "Your whole parameter is ==> ' [email protected] '"

echo "the 1st parameter ==> $"

echo "The 2nd parameter ==>"

The source program is as follows:

1Echo"The script name is ==> $"2Echo"Total parameter number is ==> $#"3["$#"-lt2] && Echo"The number of parameter is less than 2. Stop here."&& exit04Echo"Your whole parameter is ==> ' [email protected] '"5Echo"The 1st parameter ==> $"6Echo"The 2nd parameter ==>"

Create a shell file and name it shell07.sh

Enter (enter), write shell07.sh with VI editor

The results of the implementation are as follows:

Shell programming shell script default variables ($, $ ...)

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.