美文网首页
shell编程1.1: 初步认识

shell编程1.1: 初步认识

作者: 赵伯舟 | 来源:发表于2018-09-10 19:55 被阅读9次

1. 如何执行shell脚本

每个shell脚本第一行一般是#! /bin/bash,代表其实用的shell解释器。下面列举shell的几种运行方式,$代表终端提示符

$ sh script.sh
$ sh /home/path/script.sh

$ ./script.sh
$ /home/path/script.sh

$ /bin/bash script.sh

2. 在终端打印信息

使用echo:
#! /bin/bash
echo "Hello World"
echo 'Hello World'
echo Hello World

使用printf:
#! /bin/bash
printf "%-5s %-10s %-4s\n" No Name Mark
printf "%-5s %-10s %-4.2f\n" 1 Sarath 80.3456
printf "%-5s %-10s %-4.2f\n" 2 James 90.9989
printf "%-5s %-10s %-4.2f\n" 3 Jeff 77.564

带颜色的输出

在shell中设置输出的颜色很简单,只需使用\e[1;xm便可进行设置,其中x代表对应的颜色码:

echo -e "\e[1;31m This is red text \e[0m"

echo -e "\e[1;42m Green Background \e[0m"

对于字体颜色而言:reset=0, black=30, red=31, green=32, yellow=33, blue=34, magenta=35, cyan=36, and white=37
对于字体背景而言:reset = 0, black = 40, red = 41, green = 42, yellow = 43, blue = 44, magenta = 45, cyan = 46, and white=47

相关文章

网友评论

      本文标题:shell编程1.1: 初步认识

      本文链接:https://www.haomeiwen.com/subject/ofkegftx.html