Shell Script Learning Day fourth--bash internal variables, variable main strings, variable substitution

Source: Internet
Author: User
Tags eval
One, bash internal variables:

Common internal commands: Echo,eval,exec,export,readonly,read,shift, wait,exit, and Dot (.) are also the source commands.

Command function
Echo Variable Name Displays the variables specified by the variable name table to the standard output
Eval Reads a sequence of parameters, and then executes according to the characteristics of the parameters themselves.
EXEC command Parameters When the shell executes to the EXEC statement, it does not create a new child process, but instead moves to execute the new command, and when the specified command finishes, the process (that is, the original shell) terminates, so the statement after exec in the shell program is no longer executed
Export variable name =value The shell can use export to push its variable down into the child shell, allowing the child process to inherit the environment variable from the parent process, but the child shell cannot use export to bring his variable up into the parent shell
ReadOnly variable Name Reads a string from the standard input and passes it to the specified variable
Shift Offsets the position of the variable, that is, the $1,$3 becomes the $ $, each time it executes $#-1
1. Eval:

An eval can only allow the shell to process parameters one more time, so there are several more eval can be added several times.
2, Shift

3, ReadOnly

4. Exec

Exit the system directly after execution.
second, the variable main string:

Export Name= "I am Yangni"
echo ${#name}
echo ${name:2}
echo ${name:2:3}
echo ${name#i am}
Echo ${ Name%ni}
Echo ${name/yang/chun}

Application Examples:

Batch Modify file name

  for F  in ' ls *.c '    //Set F value do
     mv $f  ' echo ${f/yangni/qicheng} '
done

And , of course, there's a simpler way, that's rename.

Rename xxx yyy *.c: Put all the. c files containing the string xxx, and change the XXX to yyy

third, variable substitution: 1. ${value:-word} Determine if the variable is defined

Result=${test:-unset}: If the variable test is defined, the test content is displayed. If test is undefined, print unset (a string)

Test undefined:
[yangni@centos6 abc]$ result=${test:=unset}
[Yangni@centos6 abc]$ echo $result
UNSET

// Test definition:
[yangni@centos6 abc]$ test=3
[yangni@centos6 abc]$ result=${test:=unset}
[Yangni@centos6 abc]$ echo $result
3
2, ${value:word} if the variable value is undefined, assign word to value, ensuring that the variable always has a value.

Result=${test: =unset}: Variable not defined, assign string UNSET to test

[YANGNI@CENTOS6 abc]$ unset test
[yangni@centos6 abc]$ result=${test:=unset}
[Yangni@centos6 abc]$ Echo $test     //test Undefined, assign unset to it      
UNSET    

${value-word} is similar to 2, the variable value does not exist in Word substitution.

1./etc/init.d/httpd
2./etc/init.d/crond
3. The path operation of the variable is best to determine whether it is empty, especially the delete operation:
Path =/server/backup
Find ${path:=/tmp}-name "*.tat.gz"-type F |xargs rm-f
RM-FR ${path:=/tmp}//delete path daily programming Exercise:

#!/bin/sh 
result=1 while
[$#-GT 0]
does
   result= ' expr $result \* $ '
   shift
done
  echo $ Result

Use shift to achieve the product of all parameters. Note that the multiplication sign is escaping with escape characters, or it will get an error.

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.