Shell foundation - Overview, echo command output, the first script, script execution

Shell basic outline:

  • Shell Overview
  • Shell Script implementation
  • The basic features of the Bash
  • Bash Variables
  • Bash operator
  • Environment variable configuration file

A, Shell Overview

  • Shell is a command line interpreter, which provides a program sends a request to run the Linux kernel-level program interface system for the user, the user can use the Shell to start, suspend, stop or even write some programs.
  • Shell is still a quite powerful programming language, easy to write, easy to call, flexibility. Shell is an interpreted scripting language, you can call on Shell Linux system commands directly. All system commands can be directly called directly in the script.

Basic information of Linux supported by Shell as follows:

  • Command name: Shell
  • The path: / etc / shells (i.e., under the Shells etc)

Here Insert Picture Description

Second, the implementation of Shell script

1, echo command output

Format: [root @ localhost ~] # echo [options] [output content]

Options:

  • -e: Support backslash character conversion control;

Backslash Relevant usage:

Control characters effect
\\ Output backslash itself
\ Output warning sound
\b Backspace key to delete the left is wanted
\c Cancel newline at the end of the output line. And "-n" option as
\e ESCAPE key
\f Page breaks
\n Newline
\r enter
\t Tabs, namely the Tab key
\ v Vertical tab
\ 0nnn According to the octal ASCII character code table output. In which 0 lion zero, nnn is three octal
\ socialization According to a hexadecimal character ASCII code table output. Where hh is the two-digit hexadecimal number

for example:

  • Delete the character to the left:
[root@root ~]# echo "abc"
abc
[root@root ~]# echo -e "ab\bc"
ac
[root@root ~]# 

- newline and tab

[root@root ~]# echo -e "a\tb\tc\nd\te\tf"
a	b	c
d	e	f
  • According to hexadecimal ASCII code also can be output:
[root@root ~]# echo -e "\x61\t\x62\t\x63\n\x64\t\x65\t\x66"
a	b	c
d	e	f

Without the option of echo output. Directly add the content to be output. :

[root@root 桌面]# echo "hello world"
hello world
[root@root 桌面]# echo 'hello world'
hello world
[root@root 桌面]# 
[root@root 桌面]# echo 'hello world!'
hello world!
[root@root 桌面]# echo "hello world!"
bash: !": event not found

note:

  • If the contents of the output of a space, we should add "" (double quotation marks), if there is no space can not double quotes.
  • Exclamation point (!) In which Shell is a special effect. If the contents of the output of which has an exclamation point (!), You must remove the exclamation point with single quotes role, otherwise it will error.
  • Output Color
[root@root ~]# echo -e "\e[1;31m abcd \e[0m"
 abcd 

Format: [root @ root ~] # echo -e "\ e [1; 31m abcd \ e [0m"

  • -e ——>是特殊符号,支持反斜杠控制的字符转换;
  • \e[1 ——>开启颜色输出;
  • \e[0m ——>结束颜色输出;
  • abcd ——>表示输出的字符;
  • 31m ——>表示输出的颜色;
    #30#=黑色,31m=红色,32m=绿色,33m=黄色
    #34#=蓝色,35m=洋红,36m=青色,37m=白色

2、第一个脚本

[root@localhost sh]# vi hello.sh
#!/bin/Bash
#The first program
# Author: shenchao  (E-mail: [email protected])

echo -e "Mr. Shen chao is the most honest am in LampBroher"
  • vi hello.sh ——> 一般Linux是不区分扩展名的。若将脚本的扩展名携程.sh时,这个的作用是告诉系统,写得这个是Bash脚本。若用的不是vi编译器,而使用的是vim编译器那么会使用颜色来帮助提示编译。因此,建议将脚本写成.sh;
  • #!/bin/Bash ——> 一般在Linux中以#开始表示的是注释,但是这句话不是注释。而是标志,标称我以下写得都是Shell脚本。若脚本比较简单时,这句话不写可以的,但是若Shell脚本比较复杂嵌套其他语句,没有标称其他语句为Shell,程序运行就会报错。换句话说,这句话不能省略;
  • #The first program ——> 表示注射;

3、脚本执行

  • 赋予执行权限,直接运行;
  • 通过Bash调用执行脚本;
(1)赋予执行权限,直接运行;
  • 第一步给脚本赋予执行权限,例如:chmod 755 hello.sh ——>基于所用用户权限
  • 第二步通过绝对路径或相对路径的方式来调用例如:./hello.sh

Note: Linux among all of the executable file, whether we write Shell scripts, or binary execute the command, in order to perform must be executed with absolute or relative path.

Run the following script:

[root@root ~]# chmod 755 hello.sh
[root@root ~]# ll
总用量 104
-rwxr-xr-x. 1 root root    78 7月   5 10:13 hello.sh
zhaoliying is the most honest man in sisters
[root@root ~]# ls
anaconda-ks.cfg  install.log         公共的  视频  文档  音乐
hello.sh         install.log.syslog  模板    图片  下载  桌面
(2) call to execute the script by Bash;

Command format: [root @ root ~] # bash hello.sh

  • When the script is executed by Bash, even this we do not have execute permissions given, you can go directly to execution.
  • Meaning: to explain the script back through this bash. But still the first way we used to run;
[root@root ~]# bash hello.sh
zhaoliying is the most honest man in sisters

Guess you like

Origin blog.csdn.net/weixin_45116657/article/details/94628193