PROP_VALUE=
cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2
cat $PROPERTY_FILE | grep "$PROP_KEY" | cut -d'=' -f2
$ VAR=
"Banana"
;
$ VAR=${VAR
#?};
$
echo
$VAR;
anana
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"
while read -r num; do ((sum += num)); done < inputfile; echo $sum
if grep -q SomeString "$File"; then
Some Actions # SomeString was found
fi
cut(1)
. This example removes the first 4 characters by cutting a substring starting with 5th character.echo "$pid" | cut -c 5-
[ -d "/path/to/dir" ] && echo "Directory /path/to/dir exists."
[ ! -d "/path/to/dir" ] && echo "Directory /path/to/dir DOES NOT exists."
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
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
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
find
into a bash
array:array=()
while IFS= read -r -d $'\0'; do
array+=("$REPLY")
done < <(find . -name "${input}" -print0)
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...