适用于Bash编程初学者小例子 - 第一篇

如何声明字符串变量,并赋值?

#!/bin/bash

TARGET_CLUSTER_NODE_IP="10.245.110.69"

printf “%s \n” $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! \n"
else
     print "Different! \n"
fi

if [[ $string2 = *"Bash"* ]]
then
     printf "Contains word 'Bash'. \n"
else
     printf "Does not contain the word. \n"
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”


如何把用换行符“\n”分隔的一个大字符串,分割开,并存放到一个数组中?

#!/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=$'\n'       # 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. \n"
     printf "%s \n" "${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

https://stackoverflow.com/questions/4277665/how-do-i-compare-two-string-variables-in-an-if-statement-in-bash

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

posted on 2020-07-03 18:57  中道学友  阅读(224)  评论(0编辑  收藏  举报

导航

技术追求准确,态度积极向上