首先我们了解最基础的,输出hello world!。
1.echo 输出命令
[root@linux-study ~]echo [选项][输出内容]
选项: -e: 支持反斜线控制的字符转换
[root@linux-study ~]# echo "hello word!"
bash: !": event not found --!在shell中有特殊作用。
[root@linux-study ~]# echo "hello word"
hello word
[root@linux-study ~]# echo 'hello world!' --如果是一定要使用!,可以取消它的特殊作用。
hello world!
选项的各项作用如下:
\\ 输出\本身,其实也就是取消\后面的特殊符号
\a 输出警告音
\b 退格键,也就是向左删除键
\c 取消输出行末的换行符。和“-n”选项一致
\e ESCAPE键
\f 换页符
\n 换行符
\r 回车键
\t 制表符,也就是tab键
\v 垂直制表符
\0nnn 按照八进制ASCII码表输出字符。其中0位数字零,nnn是三位八进制数
\xhh 按照十六进制ASCII码表输出字符。其中hh是两位十六进制数
举例说明如下:
[root@linux-study ~]# echo "abc"
abc
[root@linux-study ~]# echo -e "ab\bc" --\b删除一格
ac
----------------
[root@linux-study ~]# echo -e "a\tb\tc\nd\te\tf"
a b c
d e f
----------------
[root@linux-study ~]# echo -e "\e[1;31m abcd \e[0m"
abcd
这里是带颜色字符输出。支持的格式是\e[1; 开头,\2[0m结束。支持的颜色格式有如下:
30m=黑色,31m=红色,32m=绿色,33m=黄色,34m=蓝色,35m=洋红,36m=青色,37m=白色
----------------
2.第一个脚本
root@linux-study ~]# vi
#!/bin/bash --一般情况下#开头的是注释语句,但是这里的不是,这里标示以下写的是shell程序,当然也可以不写,但嵌套其他语句时会报错,所以,以后注意,写shell时需要表明这是一个shell程序。
#The first progtam
#Author:loushen (E-mail:848814895@qq.com)
#输出一段文字
echo -e "This is my first program that i studied SHELL!"
执行脚本
[root@linux-study my-study]# sh hello.sh
This is my first program that studied SHELL
注意:一个程序,良好的注释是必要的。
3.脚本执行
(1)赋予执行权限,直接运行
chmod 755 hello.sh
./hello.sh --这种方式需要有执行权限
(2)通过bash调用执行脚本
bash hello.sh --这个也可以简写成sh hello.sh,使用bash hello.sh方式执行,不需要赋予执行权限也可以,当然如果文件不属于你就不行了
---------------------
[root@linux-study my-study]# ./hello.sh
bash: ./hello.sh: /bin/Bash: bad interpreter: No such file or directory
可能会报这个错误,原因有多种,举例其中两种:
(1)可能该文件是在windows中编译的,而windows中的换行符合linux的换行符不一样导致的。
(2)可能是没写明是#!/bin/bash导致的。
cat -A hello.sh --可以查看隐藏字符,可以了解下linux和windows的隐藏换行符的不同
[root@linux-study my-study]# cat -A hello.sh --linux本地编译的
#!/bin/bash$
#This is my first program.$
#author:loushen$
echo -e "This is my first program,hello world!"$
[root@linux-study my-study]# cat -A test.sh --windows下编译的文件,传递过来执行的
#!/bin/bash^M$
#This is my first program.^M$
#author:loushen^M$
echo -e "This is my first program,hello world!"^M$
这种windows是dos,而linux则不同,在linux可以直接进行转换
dos2unix test.sh --dos2unix,这里的2就是英文中to,均可以这样理解
然后就可以执行./test.sh文件了
---------------------
1.echo 输出命令
[root@linux-study ~]echo [选项][输出内容]
选项: -e: 支持反斜线控制的字符转换
[root@linux-study ~]# echo "hello word!"
bash: !": event not found --!在shell中有特殊作用。
[root@linux-study ~]# echo "hello word"
hello word
[root@linux-study ~]# echo 'hello world!' --如果是一定要使用!,可以取消它的特殊作用。
hello world!
选项的各项作用如下:
\\ 输出\本身,其实也就是取消\后面的特殊符号
\a 输出警告音
\b 退格键,也就是向左删除键
\c 取消输出行末的换行符。和“-n”选项一致
\e ESCAPE键
\f 换页符
\n 换行符
\r 回车键
\t 制表符,也就是tab键
\v 垂直制表符
\0nnn 按照八进制ASCII码表输出字符。其中0位数字零,nnn是三位八进制数
\xhh 按照十六进制ASCII码表输出字符。其中hh是两位十六进制数
举例说明如下:
[root@linux-study ~]# echo "abc"
abc
[root@linux-study ~]# echo -e "ab\bc" --\b删除一格
ac
----------------
[root@linux-study ~]# echo -e "a\tb\tc\nd\te\tf"
a b c
d e f
----------------
[root@linux-study ~]# echo -e "\e[1;31m abcd \e[0m"
abcd
这里是带颜色字符输出。支持的格式是\e[1; 开头,\2[0m结束。支持的颜色格式有如下:
30m=黑色,31m=红色,32m=绿色,33m=黄色,34m=蓝色,35m=洋红,36m=青色,37m=白色
----------------
2.第一个脚本
root@linux-study ~]# vi
#!/bin/bash --一般情况下#开头的是注释语句,但是这里的不是,这里标示以下写的是shell程序,当然也可以不写,但嵌套其他语句时会报错,所以,以后注意,写shell时需要表明这是一个shell程序。
#The first progtam
#Author:loushen (E-mail:848814895@qq.com)
#输出一段文字
echo -e "This is my first program that i studied SHELL!"
执行脚本
[root@linux-study my-study]# sh hello.sh
This is my first program that studied SHELL
注意:一个程序,良好的注释是必要的。
3.脚本执行
(1)赋予执行权限,直接运行
chmod 755 hello.sh
./hello.sh --这种方式需要有执行权限
(2)通过bash调用执行脚本
bash hello.sh --这个也可以简写成sh hello.sh,使用bash hello.sh方式执行,不需要赋予执行权限也可以,当然如果文件不属于你就不行了
---------------------
[root@linux-study my-study]# ./hello.sh
bash: ./hello.sh: /bin/Bash: bad interpreter: No such file or directory
可能会报这个错误,原因有多种,举例其中两种:
(1)可能该文件是在windows中编译的,而windows中的换行符合linux的换行符不一样导致的。
(2)可能是没写明是#!/bin/bash导致的。
cat -A hello.sh --可以查看隐藏字符,可以了解下linux和windows的隐藏换行符的不同
[root@linux-study my-study]# cat -A hello.sh --linux本地编译的
#!/bin/bash$
#This is my first program.$
#author:loushen$
echo -e "This is my first program,hello world!"$
[root@linux-study my-study]# cat -A test.sh --windows下编译的文件,传递过来执行的
#!/bin/bash^M$
#This is my first program.^M$
#author:loushen^M$
echo -e "This is my first program,hello world!"^M$
这种windows是dos,而linux则不同,在linux可以直接进行转换
dos2unix test.sh --dos2unix,这里的2就是英文中to,均可以这样理解
然后就可以执行./test.sh文件了
---------------------