1. 如何執(zhí)行shell腳本
每個(gè)shell腳本第一行一般是#! /bin/bash
柠硕,代表其實(shí)用的shell解釋器。下面列舉shell的幾種運(yùn)行方式,$
代表終端提示符
$ 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中設(shè)置輸出的顏色很簡(jiǎn)單,只需使用\e[1;xm
便可進(jìn)行設(shè)置,其中x
代表對(duì)應(yīng)的顏色碼:
echo -e "\e[1;31m This is red text \e[0m"
echo -e "\e[1;42m Green Background \e[0m"
對(duì)于字體顏色而言:reset=0, black=30, red=31, green=32, yellow=33, blue=34, magenta=35, cyan=36, and white=37
對(duì)于字體背景而言:reset = 0, black = 40, red = 41, green = 42, yellow = 43, blue = 44, magenta = 45, cyan = 46, and white=47