Friday, 13 August 2021

Make a symbolic link to a relative pathname

 If you create a symbolic link to a relative path, it will store it as a relative symbolic link, not absolute like your example shows. This is generally a good thing. Absolute symbolic links don't work when the filesystem is mounted elsewhere.

The reason your example doesn't work is that it's relative to the parent directory of the symbolic link and not where ln is run.

You can do:

$ pwd
/home/beau
$ ln -s foo/bar.txt bar.txt
$ readlink -f /home/beau/bar.txt
/home/beau/foo/bar.txt

Or for that matters:

$ cd foo
$ ln -s foo/bar.txt ../bar.txt


from: https://unix.stackexchange.com/questions/10370/make-a-symbolic-link-to-a-relative-pathname

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

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...