Linux special Variables

Source: Internet
Author: User

$ # Parameter serial number sent to the shell command

$-Options are provided when shell is started or the set command is used

$? Value returned after the previous command is executed

$ Process ID of the Current Shell

$! Process ID of the previous sub-process

$ @ All parameters, each enclosed in double brackets

$ * All parameters, enclosed in double brackets

$ N location parameter value; n indicates location

$0 Current Shell Name

$ * All command line parameters

$ @ Same as above

"$ *" Treats all command line parameters as one

"$ @" Gets the entire parameter list and separates it into different parameters.

 

[Example]

For VaR in $ * # Try $ *, $ @, "$ *", "$ @"

Do

Echo $ VaR

Done

The following script backs up all the target files to be edited to the/bak directory, and keeps all paths unchanged, such as VII/etc/hosts, copy/etc/hosts to/bak/etc/hosts.

# Cat/SH/VII

#! /Usr/bin/bash

# Check whether the target file exists before performing the backup operation

If [-F "$1"]; then

# Create a function to enable any key to continue

# If stty raw is not recognized on your machine, replace the raw in the two parts of the function with cbreak.

Get_char ()

{

Savedstty = 'stty-G'

Stty-echo

Stty raw

Dd If =/dev/tty BS = 1 COUNT = 1 2>/dev/null

Stty-raw

Stty echo

Stty $ savedstty

}

# Create the backup root path/bak and grant everyone the write permission

If [! -D "/bak"]; then

Mkdir/bak

Chmod A + w/bak

Fi

# Determine the path of the target file and create it so that everyone has the write permission on any newly created level directory.

Echo $1 | grep "/">/dev/null

If ["'echo $? '"=" 0 "]; then

CD $ {1% /*}

Fi

Pwdnow = 'pwd'

Path =/bak $ {pwdnow}

# Echo $ path

If [! -D "$ path"]; then

Mkdir-p $ path

Patha =$ {pwdnow #/}

Chmod-r a + w/bak/$ {patha % /*}

Fi

# Copying the target file

Datenow = 'date + % Y % m % d'

Timenow = 'date + % H % m % s'

/Usr/bin/CP $1 $ {path}/$ {1 # */}. $ {datenow}. $ {timenow}

Echo "target file \" $1 \ "has been copied to $ {path}/$ {1 # */}. $ {datenow}. $ {timenow }"

Echo "now going to \" VI $1 \ ", Press any key to continue ..."

Char = 'get _ char'

Fi

# Call the VI command

VI $1

Call scripts

# Chmod A + x/SH/VII

#/SH/VII/etc/passwd

You can add/sh to the path variable to make it easier.

Reprinted from http://xuxd32.blog.163.com/blog/static/48121933200922123138638/

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.