Thursday, 29 July 2021

Replace one substring for another string in shell script

 To replace the first occurrence of a pattern with a given string, use ${parameter/pattern/string}:

#!/bin/bash
firstString="I love Suzi and Marry"
secondString="Sara"
echo "${firstString/Suzi/$secondString}"    
# prints 'I love Sara and Marry'

To replace all occurrences, use ${parameter//pattern/string}:

message='The secret code is 12345'
echo "${message//[0-9]/X}"           
# prints 'The secret code is XXXXX'


from: https://stackoverflow.com/questions/13210880/replace-one-substring-for-another-string-in-shell-script

Tuesday, 27 July 2021

Bash if statement with multiple conditions throws an error

 Use -a (for and) and -o (for or) operations.

tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html

Update

Actually you could still use && and || with the -eq operation. So your script would be like this:

my_error_flag=1
my_error_flag_o=1
if [ $my_error_flag -eq 1 ] ||  [ $my_error_flag_o -eq 2 ] || ([ $my_error_flag -eq 1 ] && [ $my_error_flag_o -eq 2 ]); then
      echo "$my_error_flag"
else
    echo "no flag"
fi

Although in your case you can discard the last two expressions and just stick with one or operation like this:

my_error_flag=1
my_error_flag_o=1
if [ $my_error_flag -eq 1 ] ||  [ $my_error_flag_o -eq 2 ]; then
      echo "$my_error_flag"
else
    echo "no flag"
fi


from: https://stackoverflow.com/questions/16203088/bash-if-statement-with-multiple-conditions-throws-an-error

Monday, 26 July 2021

How to check if a string contains a substring in Bash

string='My long string'
if [[ $string == *"My long"* ]]; then
  echo "It's there!" 
fi 


from: https://stackoverflow.com/questions/229551/how-to-check-if-a-string-contains-a-substring-in-bash

Saturday, 24 July 2021

Return a particular value if the given array value doesn't exist

 if [ "${backups[$service]:-NOT_HERE}" != "NOT_HERE" ]; then

    # do what you want if the value does exist in the array
fi


from: https://stackoverflow.com/questions/44823572/get-an-element-from-a-associative-array-in-bash-with-set-u

Remove an element from a Bash array

The following works as you would like in bash and zsh:

$ array=(pluto pippo)
$ delete=pluto
$ echo ${array[@]/$delete}
pippo
$ array=( "${array[@]/$delete}" ) #Quotes when working with strings

If need to delete more than one element:

...
$ delete=(pluto pippo)
for del in ${delete[@]}
do
   array=("${array[@]/$del}") #Quotes when working with strings 

done 


from: https://stackoverflow.com/questions/16860877/remove-an-element-from-a-bash-array

How can I join elements of an array in Bash?

 #!/bin/bash

foo=('foo bar' 'foo baz' 'bar baz')
bar=$(printf ",%s" "${foo[@]}")
bar=${bar:1}

echo $bar


from: https://stackoverflow.com/questions/1527049/how-can-i-join-elements-of-an-array-in-bash/53050617

Thursday, 22 July 2021

To check if a directory exists in a shell script

 if [ -d "$DIRECTORY" ]; then

  # Control will enter here if $DIRECTORY exists.
fi

Or to check if a directory doesn't exist:

if [ ! -d "$DIRECTORY" ]; then
  # Control will enter here if $DIRECTORY doesn't exist.
fi

Add a new element to an array without specifying the index in Bash

ARRAY=()
ARRAY+=('foo')
ARRAY+=('bar')


from: https://stackoverflow.com/questions/1951506/add-a-new-element-to-an-array-without-specifying-the-index-in-bash

Wednesday, 14 July 2021

How To Run a Command Multiple Times in Terminal and PowerShell

# Print all the numbers from 1 to 100
for i in {1..100}; do echo ${i}; done
# Run the "e2e-tests" script 5 times to verify tests work reliably
for i in {1..5}; do npm run e2e-tests; done
# Run the "deploy-app-1/2/3/..." scripts to deploy the app to different servers

for i in {1..10}; do npm run deploy-app-${i}; done 


from: https://betterprogramming.pub/how-to-run-a-command-multiple-times-in-terminal-and-powershell-5af76df8d123

How to check if a file contains a specific string using Bash

 In case if you want to check whether file does not contain a specific string, you can do it as follows. if ! grep -q SomeString "$File...