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
网友评论