ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2020-07-03 19:04:59  阅读:365  来源: 互联网

标签:bin 编程 results echo 初学者 flush array Bash 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

标签:bin,编程,results,echo,初学者,flush,array,Bash,bash
来源: https://www.cnblogs.com/awpatp/p/13232251.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有