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

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

Tuesday, 3 November 2020

Samsung 4k電視與NSwitch HDMI不相容解法

 簡單說結論

在switch的設定頁面 最左下倒數第二個 有個"電視輸出"的選項 右側第二個選項是 RGB xxx 把她選成"限制"就搞定了 (不可以選自動 標準)


from : https://pttgame.com/nswitch/M.1573799140.A.D3D.html

Thursday, 29 October 2020

Jenkins __pycache__/__init__.cpython-37.pyc: Operation not permitted

 sudo chown -R jenkins:jenkins /var/lib/jenkins/workspace


from : https://stackoverflow.com/questions/50782740/why-is-jenkins-suddenly-unable-to-delete-a-workspace

Thursday, 22 October 2020

Tuesday, 20 October 2020

How do I copy a string to the clipboard using Python?

 The simplest way is with pyperclip. Works in python 2 and 3.

To install this library, use:

pip install pyperclip

Example usage:

import pyperclip

pyperclip.copy("your string")

If you want to get the contents of the clipboard:

clipboard_content = pyperclip.paste()


from : 

https://stackoverflow.com/questions/579687/how-do-i-copy-a-string-to-the-clipboard-on-windows-using-python

https://stackoverflow.com/questions/45014501/trying-to-write-copied-data-in-a-text-file-in-python

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