Tuesday, 28 June 2022

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"; then

  Some Actions # SomeString was not found

fi


from: https://stackoverflow.com/questions/11287861/how-to-check-if-a-file-contains-a-specific-string-using-bash

Tuesday, 7 June 2022

sed command with -i option failing on Mac, but works on Linux

 I believe on OS X when you use -i an extension for the backup files is required. Try:

sed -i .bak 's/hello/gbye/g' *

Using GNU sed the extension is optional.


from: https://stackoverflow.com/questions/4247068/sed-command-with-i-option-failing-on-mac-but-works-on-linux

Thursday, 21 April 2022

git auto-complete for *branches* at the command line?

 ok, so I needed the git autocompletion script.

I got that from this url:

curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash

No need to worry about what directory you're in when you run this as your home directory(~) is used with the target.

Then I added to my ~/.bash_profile file the following 'execute if it exists' code:

if [ -f ~/.git-completion.bash ]; then
  . ~/.git-completion.bash
fi

Update: I'm making these bits of code more concise to shrink down my .bashrc file, in this case I now use:

test -f ~/.git-completion.bash && . $_

Note: $_ means the last argument to the previous command. so . $_ means run it - "it" being .git-completion.bash in this case

This still works on both Ubuntu and OSX and on machines without the script .git-completion.bash script.

Now git Tab (actually it's git TabTab ) works like a charm!

p.s.: If this doesn't work off the bat, you may need to run chmod u+x ~/.git-completion.bash to grant yourself the necessary permission:

  • chmod is the command that modifies file permissions
  • u means the user that owns the file, by default its creator, i.e. you
  • + means set/activate/add a permission
  • x means execute permission, i.e. the ability to run the script


from: https://apple.stackexchange.com/questions/55875/git-auto-complete-for-branches-at-the-command-line

Execute bash shell in Makefile

 status:

    eval $$(docker-machine env dev); docker-compose ps

Trying to embed newline when concat two string variables in Bash

 

  1. Inserting \n

     p="${var1}\n${var2}"
     echo -e "${p}"
    
  2. Inserting a new line in the source code

     p="${var1}
     ${var2}"
     echo "${p}"
    
  3. Using $'\n' (only Bash and Z shell)

     p="${var1}"$'\n'"${var2}"
     echo "${p}"


from: https://stackoverflow.com/questions/9139401/trying-to-embed-newline-in-a-variable-in-bash

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

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