Thursday, 16 July 2020

How to read a file into a variable in shell?

In cross-platform, lowest-common-denominator sh you use:

#!/bin/sh
value=`cat config.txt`
echo "$value"

In bash or zsh, to read a whole file into a variable without invoking cat:

#!/bin/bash
value=$(<config.txt)
echo "$value"

Bash command to sum a column of numbers

while read -r num; do ((sum += num)); done < inputfile; echo $sum

from : https://stackoverflow.com/questions/3096259/bash-command-to-sum-a-column-of-numbers

Tuesday, 7 July 2020

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

if grep -q SomeString "$File"; then
  Some Actions # SomeString was found
fi

Source .bashrc in zsh without printing any output

source ~/.bashrc > /dev/null 2>&1

How can I strip first X characters from string using sed?

Here's a concise method to cut the first X characters using cut(1). This example removes the first 4 characters by cutting a substring starting with 5th character.
echo "$pid" | cut -c 5-

from : https://stackoverflow.com/questions/11469989/how-can-i-strip-first-x-characters-from-string-using-sed

HowTo: Check If a Directory Exists In a Shell Script

How do I check if a directory exists in a shell script under Linux or Unix like operating systems? How can I check if a directory exists in a shell script on Unix-like system?

To check if a directory exists in a shell script and is a directory use the following syntax:
[ -d "/path/to/dir" ] && echo "Directory /path/to/dir exists."
OR
[ ! -d "/path/to/dir" ] && echo "Directory /path/to/dir DOES NOT exists."

Fix No value for $TERM and no -T specified

What ultimately worked for me was to check whether the shell was an interactive shell. I based the solution on this other post at unix.stackexchange: How to check if a shell is login/interactive/batch.

So the code for the solution was:

if [[ $- == *i* ]]; then
  fgRed=$(tput setaf 1)     ; fgGreen=$(tput setaf 2)  ; fgBlue=$(tput setaf 4)
  fgMagenta=$(tput setaf 5) ; fgYellow=$(tput setaf 3) ; fgCyan=$(tput setaf 6)
  fgWhite=$(tput setaf 7)   ; fgBlack=$(tput setaf 0)
  bgRed=$(tput setab 1)     ; bgGreen=$(tput setab 2)  ; bgBlue=$(tput setab 4)
  bgMagenta=$(tput setab 5) ; bgYellow=$(tput setab 3) ; bgCyan=$(tput setab 6)
  bgWhite=$(tput setab 7)   ; bgBlack=$(tput setab 0)
fi

Shell/Bash - How to save array to file and another file array load?

If the values of your variables are not in multiple lines, a basic and easy way for it is to use set:

# Save
set | grep ^ARR= > somefile.arrays
# Load
. somefile.arrays

How to exclude a directory in find . command

find -name "*.js" -not -path "./directory/*"

Loop through an array of strings in Bash?

You can use it like this:

## declare an array variable
declare -a arr=("element1" "element2" "element3")

## now loop through the above array
for i in "${arr[@]}"
do
   echo "$i"
   # or do whatever with individual element of the array
done

# You can access them using echo "${arr[0]}", "${arr[1]}" also

How can I store the “find” command results as an array in Bash

Answer for bash 4.3 or earlier

Here is one solution for getting the output of find into a bash array:
array=()
while IFS=  read -r -d $'\0'; do
    array+=("$REPLY")
done < <(find . -name "${input}" -print0)


from : https://stackoverflow.com/questions/23356779/how-can-i-store-the-find-command-results-as-an-array-in-bash

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