- Bash Cookbook
- Ron Brash Ganesh Naik
- 266字
- 2021-07-23 19:17:37
How to do it...
Let's begin our activity as follows:
- Open up a new script called echo-mayhem.sh in your favorite editor and a new terminal. Enter the following contents and then execute the script at the prompt:
#!/bin/bash
# What about echo?
echo -n "Currently we have seen the command \"echo\" used before"
echo " in the previous script"
echo
echo -n "Can we also have \t tabs? \r\n\r\n?"
echo " NO, not yet!"
echo
echo -en "Can we also have \t tabs? \r\n\r\n?"
echo " YES, we can now! enable interpretation of backslash escapes"
echo "We can also have:"
echo -en '\xF0\x9F\x92\x80\n' # We can also use \0NNN for octal instead of \xFF for hexidecimal
echo "Check the man pages for more info ;)"
- After reviewing the results of echo-mayhem.sh, create another script called printf-mayhem and enter the following contents:
#!/bin/bash
export LC_NUMERIC="en_US.UTF-8"
printf "This is the same as echo -e with a new line (\\\n)\n"
DECIMAL=10.0
FLOAT=3.333333
FLOAT2=6.6666 # On purpose two missing values
printf "%s %.2f\n\n" "This is two decimal places: " ${DECIMAL}
printf "shall we align: \n\n %.3f %-.6f\n" ${FLOAT} ${FLOAT2}
printf " %10f %-6f\n" ${FLOAT} ${FLOAT2}
printf " %-10f %-6f\n" ${FLOAT} ${FLOAT2}
# Can we also print other things?
printf '%.0s-' {1..20}; printf "\n"
# How about we print the hex value and a char for each value in a string?
STR="No place like home!"
CNT=$(wc -c <<< $STR})
TMP_CNT=0
printf "Char Hex\n"
while [ ${TMP_CNT} -lt $[${CNT} -1] ]; do
printf "%-5s 0x%-2X\n" "${STR:$TMP_CNT:1}" "'${STR:$TMP_CNT:1}"
TMP_CNT=$[$TMP_CNT+1]
done
- Execute the contents of printf-mayhem.sh and review the contents for subtle differences.
推薦閱讀
- 基于粒計(jì)算模型的圖像處理
- Getting Started with CreateJS
- UML+OOPC嵌入式C語(yǔ)言開(kāi)發(fā)精講
- Unity 5.x By Example
- Learning Salesforce Einstein
- Selenium Testing Tools Cookbook(Second Edition)
- Oracle實(shí)用教程
- Learning iOS Security
- 數(shù)據(jù)分析與挖掘算法:Python實(shí)戰(zhàn)
- Java EE架構(gòu)設(shè)計(jì)與開(kāi)發(fā)實(shí)踐
- 原型設(shè)計(jì):打造成功產(chǎn)品的實(shí)用方法及實(shí)踐
- Visual FoxPro程序設(shè)計(jì)習(xí)題及實(shí)驗(yàn)指導(dǎo)
- Learn C Programming
- C語(yǔ)言程序設(shè)計(jì)實(shí)驗(yàn)指導(dǎo)教程
- C語(yǔ)言從入門到精通(第5版)