适用于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   中道学友  阅读(225)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2019-07-03 Can't locate Math/Round.pm in @INC
2019-07-03 用cp命令拷贝文件,源目录后带不带斜杠的区别
2014-07-03 Fibre Channel和Fiber Channel
2014-07-03 Backup and restore of FAST Search for SharePoint 2010
2010-07-03 DB server name OR DB cluster name?
2010-07-03 The call to LoadLibrary(YourExtensionName) failed, Win32 error 0n14001, “This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem.”

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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

点击右上角即可分享
微信分享提示