Shell First lesson study notes

Source: Internet
Author: User

Shell First lesson study notes

    1. What do you mean shell?

      The shell is a command interpreter that, at the outermost layer of the operating system, is responsible for directly talking to the user, explaining the commands entered by the user to

The operating system and handles the output of a wide variety of operations, output to the screen back to the user (interactive or non-interactive).


Case 1. Simply clear the Messages log script under/var/log:

#!bin/bashcd/var/logcat/dev/null > Messagesecho "Logs cleaned up"

Case 2: Contains variables, commands, Process Control statements clear/var/log messages log simple script :

#!bin/bashlog_dir=/var/logroot_uid=0if [$UID-ne $ROOT _uid]echo "You must being ROOT to runing this csript" Exit 1ficd $LOG _dir | | {echo "Cannot change to necessary directory" ##| | The preceding command does not succeed in implementing the brackets in the command exit 1}cat/dev/null > Messages && echo "Log cleaned up" ##&& the previous command succeeded in implementing the following command E XIT 0

2. There are three ways of executing scripts

A.bash script-name or SH script-name

B.path/script-name or./script-name (current path implementation)

C.source script-name or. script-name

The C-type division usually uses source or "." Reads or loads the specified shell script file, and then executes all the statements in the specified shell script. These statements run as part of the current parent shell script process. Therefore, use Soucrce or "." A point number can be used to pass the return value of a variable in the script's own script, such as a value or function, to the current parent shell script. This is the biggest difference in the third way.

SOURCE or "." The function of the dot command is to execute source or "." in the current shell. Instead of generating a child shell to execute commands in the command file, the DOT code loads and executes the commands and statements in the associated script file.

Include a fairly source or dot number in PHP development.

The case description is as follows:

[[email protected] jiaoben]# cat >tesh.shecho  "I am xiaoping" ^C[[email  protected] jiaoben]# sh tesh.shI am xiaoping[[email protected]  jiaoben]# .tesh.sh  #点后面需要空格-bash: .tesh.sh: command not found[[email  protected] jiaoben]# ./tesh.sh  #没有执行权限-bash: ./tesh.sh: permission denied[[email  protected] jiaoben]# source tesh.shI am xiaoping[[email protected]  jiaoben]# chmod + tesh.sh[[email protected] jiaoben]# chmod +x tesh.sh [[email protected] jiaoben]# ./tesh.shi am xiaoping[[email protected]  jiaoben]# source tesh.shi am xiaoping[[email protected] jiaoben]# ./1.10.sh     break.sh   jiance.sh  prinf.sh   tesh.sh     zifu.sh[[email protected] jiaoben]# ./1.10.sh    break.sh   jiance.sh   prinf.sh   tesh.sh    zifu.sh[[email protected]  jiaoben]# echo  ' userdir= ' pwd '  >testsource.sh[[email protected] jiaoben]# cat  testsource.shuserdir= ' pwd ' [[email protected] jiaoben]# sh testsource.sh  # Direct load Execution Script [[email protected] jiaoben]# echo  $userdir    #并没有加载到文件的变量 [[email  protected] jiaoben]# . testsource.sh[[email protected] jiaoben]# echo $ userdir # #没有变量后面没有 ' ' symbol to output directly pwd[[email protected] jiaoben]# cat testsource.shuserdir= pwd[[email protected] jiaoben]# pwd/jiaoben[[email protected] jiaoben]# vi  shshengri.sh  shif.sh[[email protected] jiaoben]# vi testsource.sh[[email  Protected] jiaoben]#&nbSP;.  testsource.sh   #直接加载执行脚本 [[email protected] jiaoben]# echo  $userdir      #可以加载脚本的变量, Output screen/jiaoben# the current window is a shell, and the script testsource.sh is another shell, when using sh  testsource.sh execution is a variable that has already been defined, is not defined to pass to the current window, nor does this function. With.  testsource.sh You can use the definition variable to pass to the current shell to output the screen. If you want to load the variables in the script, it should be used. or source.


This article is from the "Little K" blog, make sure to keep this source http://zyp88.blog.51cto.com/1481591/1606534

Shell First lesson study notes

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.