适用于Bash编程初学者小例子 - 第一篇
Posted awpatp
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了适用于Bash编程初学者小例子 - 第一篇相关的知识,希望对你有一定的参考价值。
如何声明字符串变量,并赋值?
#!/bin/bash
TARGET_CLUSTER_NODE_IP="10.245.110.69"
printf “%s ” $TARGET_CLUSTER_NODE_IP
如何初始化一个字符串数组变量,并遍历输出其每一个字符串元素的值?
#!/bin/bash
declare -a string_array=("Hello world!" "How are you?" "Nice to meet you!" "How do you do?" )
# Read the array values with space
for str in "${string_array[@]}"; do
echo $str
done
如何SSH到另一台主机并在其上运行几个命令?
#!/bin/bash
# 先配置SSH的免密,之后执行此脚本
hostname
ssh root@10.245.110.69 ‘hostname; whoami; date‘
hostname
如何使用IF语句比较字符串的值?
#!/bin/bash
string1="Hello World!"
string2="Hello Bash!"if [[ $string1 == "Hello World!" ]]
then
printf "Same! "
else
print "Different! "
fiif [[ $string2 = *"Bash"* ]]
then
printf "Contains word ‘Bash‘. "
else
printf "Does not contain the word. "
fi
如何把for语句放在一行里执行?
#!/bin/bash
# For statement in multiple lines
for((i=1;i<10;i+=2))
do
echo "Welcome $i times"
done
# For statement in one line
for((i=1;i<=10;i+=2)); do echo "Welcome $i times"; done
如何建立一个字符串数组,并且查看每一个字符串?
#!/bin/bash
declare -a flush_results
flush_results+=("1.Hello")
flush_results+=("2.World")
flush_results+=("3.Bash")
echo ${flush_results[0]}
echo ${flush_results[1]}
echo ${flush_results[2]}
echo ${flush_results[@]}
如何捕获一个命令的输出,并把输出存到一个变量里?
#!/bin/bash
declare OUTPUT=$(ssh root@10.111.111.111 isi_for_array isi_flush --dedupe-queue --dedupe-index )
# Output of command "#ssh root@10.111.111.111 isi_for_array isi_flush --dedupe-queue --dedupe-index":
#f810-4: Cache flushing complete.
#f810-3: Cache flushing complete.
#f810-2: Cache flushing complete.
#f810-1: Cache flushing complete.
#Show result in multi-lines
echo "$OUTPUT"
#Show result in one line
echo $OUTPUT
*为什么OUTPUT带不带双引号,区别这么大呢?
Jonathan Leffler解释如下:
“the difference is that (1) the double-quoted version of the variable (echo "$VARIABLE") preserves internal spacing of the value exactly as it is represented in the variable — newlines, tabs, multiple blanks and all — whereas (2) the unquoted version (echo $VARIABLE) replaces each sequence of one or more blanks, tabs and newlines with a single space. Thus (1) preserves the shape of the input variable, whereas (2) creates a potentially very long single line of output”
如何把用换行符“ ”分隔的一个大字符串,分割开,并存放到一个数组中?
#!/bin/bash
declare OUTPUT=$(ssh root@10.111.111.111 isi_for_array isi_flush --dedupe-queue --dedupe-index )
echo "$OUTPUT"
echo $OUTPUT
SAVEIFS=$IFS # Save current IFS
IFS=$‘ ‘ # Change IFS to new line
names=($OUTPUT) # split to array $names
IFS=$SAVEIFS # Restore IFS
for ((j=0;j<4;j+=1)); do
printf "Current is line: $j. "
printf "%s " "${names[$j]}"
done
参考资料
=============
https://linuxconfig.org/bash-printf-syntax-basics-with-examples
https://linuxhint.com/bash_loop_list_strings/
https://www.shellhacks.com/ssh-execute-remote-command-script-linux/
https://wangchujiang.com/linux-command/c/awk.html
https://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858470.html
https://ryanstutorials.net/bash-scripting-tutorial/bash-if-statements.php
http://www.masteringunixshell.net/qa36/bash-how-to-add-to-array.html
https://www.cyberciti.biz/faq/linux-unix-bash-for-loop-one-line-command/
https://www.claudiokuenzler.com/blog/762/bash-multi-line-output-saved-one-line-variable
https://stackoverflow.com/questions/24628076/bash-convert-n-delimited-strings-into-array/45565601
以上是关于适用于Bash编程初学者小例子 - 第一篇的主要内容,如果未能解决你的问题,请参考以下文章
linux shell编程bash编程shell教程bash教程shell文档bash文档shell脚本bash脚本教程第一部分:绪论
适用于 Windows 的 Git:Git\bin\bash.exe 和 Git\usr\bin\bash.exe 之间的区别