bash4

Multiple matches in a string using regex in bash

时光怂恿深爱的人放手 提交于 2019-12-01 20:52:57
Been looking for some more advanced regex info on regex with bash and have not found much information on it. Here's the concept, with a simple string: myString="DO-BATCH BATCH-DO" if [[ $myString =~ ([[:alpha:]]*)-([[:alpha:]]*) ]]; then echo ${BASH_REMATCH[1]} #first perens echo ${BASH_REMATCH[2]} #second perens echo ${BASH_REMATCH[0]} #full match fi outputs: BATCH DO DO-BATCH So fine it does the first match (BATCH-DO) but how do I pull a second match (DO-BATCH)? I'm just drawing a blank here and can not find much info on bash regex. OK so one way I did this is to put it in a for loop:

Unable to enable globstar in Bash 4

本秂侑毒 提交于 2019-11-29 10:03:21
I put the following unsuccessfully to my .bashrc shopt -s globstar I am trying to test the command in action by ls **/*.c and by comparing it to ls */*/*.c How can you enable globstar in Bash 4? Hmm. shopt -s globstar should work. To debug, make sure you are running Bash 4: $SHELL --version Then check the setting of globstar : shopt globstar If it is unset, try setting it manually: shopt -s globstar Now see if that works. If it does, you might want to look into why your .bashrc isn't working. Did you remember to restart you shell after editing your .bashrc , or load it with . .bashrc ? 来源:

Unable to enable globstar in Bash 4

微笑、不失礼 提交于 2019-11-28 03:32:38
问题 I put the following unsuccessfully to my .bashrc shopt -s globstar I am trying to test the command in action by ls **/*.c and by comparing it to ls */*/*.c How can you enable globstar in Bash 4? 回答1: Hmm. shopt -s globstar should work. To debug, make sure you are running Bash 4: $SHELL --version Then check the setting of globstar : shopt globstar If it is unset, try setting it manually: shopt -s globstar Now see if that works. If it does, you might want to look into why your .bashrc isn't

bash4 read file into associative array

帅比萌擦擦* 提交于 2019-11-27 15:11:10
问题 I am able to read file into a regular array with a single statement: local -a ary readarray -t ary < $fileName Not happening is reading a file into assoc. array. I have control over file creation and so would like to do as simply as possible w/o loops if possible at all. So file content can be following to be read in as: keyname=valueInfo But I am willing to replace = with another string if cuts down on code, especially in a single line code as above. And ... So would it be possible to read

How to iterate over associative arrays in Bash

喜你入骨 提交于 2019-11-26 16:51:27
Based on an associative array in a Bash script, I need to iterate over it to get the key and value. #!/bin/bash declare -A array array[foo]=bar array[bar]=foo I actually don't understand how to get the key while using a for-in loop. The keys are accessed using an exclamation point: ${!array[@]} , the values are accessed using ${array[@]} . You can iterate over the key/value pairs like this: for i in "${!array[@]}" do echo "key : $i" echo "value: ${array[$i]}" done Note the use of quotes around the variable in the for statement (plus the use of @ instead of * ). This is necessary in case any