How To?
Manipulate File
Check if a file exist
if [ ! -f /tmp/foo.txt ]; then
echo "File not found!"
fiFind all files with specific extension of a directory and its subdirectories
find . -type f -name "*.conf"Parse file with shell script
#!/bin/sh
set -e
cmd="/usr/local/bin/docker-entrypoint.sh"
configFile="/usr/config/memcached.conf"
# parseConfig parse the config file and convert it to argument to pass to memcached binary
parseConfig() {
args=""
while read -r line || [ -n "$line" ]; do
case $line in
-*) # for config format -c 500 or --conn-limit=500
args="$(echo "${args}" "${line}")"
;;
[a-z]*) # for config format conn-limit = 500
trimmedLine="$(echo "${line}" | tr -d '[:space:]')" # trim all spaces from the line (i.e conn-limit=500)
param="$(echo "--${trimmedLine}")" # append -- in front of trimmedLine (i.e --conn-limit=500)
args="$(echo "${args}" "${param}")"
;;
\#*) # line start with #
# commented line, ignore it
;;
*) # invalid format
echo "\"$line\" is invalid configuration parameter format"
echo "Use any of the following format\n-c 300\nor\n--conn-limit=300\nor\nconn-limit = 300"
exit 1
;;
esac
done <"$configFile"
cmd="$(echo "${cmd}" "${args}")"
}
# if configFile exist then parse it.
if [ -f "${configFile}" ]; then
parseConfig
fi
# Now run docker-entrypoint.sh and send the parsed configs as arguments to it
$cmdCreate lots of random files
Manipulate String
Check first character of a string
Using wildcard
Using substring expansion
This is going to take a substring of str starting at the 0th character with length 1.
Using Regex
^ indicates starting with.
Remove space from a string
space from a stringCheck if a string/line exist in a file
Here,
F: Affects how PATTERN is interpreted (fixed string instead of a regex)
x: Match whole line
q: Shhhhh... minimal printing
Example:
This check if out.yaml file has a line starting with $prefix.
Replace whole line in of a file that match a pattern
Here, -i for replace in place and c is for change/replace.
Example:
This replace the line that start with $key of out.yaml file with $line.
Use Bearer Token
With wget
Example:
With curl
Example:
Work with Executable
Check an executable file static build or dynamic build
Use this command to check if a file is statistically linked or dynamically linked.
or
Last updated
Was this helpful?