Skip to content

Instantly share code, notes, and snippets.

@vheidari
Last active May 9, 2023 16:38
Show Gist options
  • Save vheidari/0719cb43f2fcef23ca707ec1b7618faf to your computer and use it in GitHub Desktop.
Save vheidari/0719cb43f2fcef23ca707ec1b7618faf to your computer and use it in GitHub Desktop.
Bash [ Commands / Scripts ] Tricks
#!/bin/bash
# Encoding and Decoding a string with Base64 command.
# Note: you cant pass string directly to Base64 in below example we pass string as output string with pipe.
# Note: $# holds numbers of argument that passed to ShellScript. inside function scope it also holds number of argumant that
# passed to function.
e64() {
if [[ $# == 0 ]]; then
echo ""
else
printf '%s' $1 | base64
fi
}
d64() {
if [[ $# -eq 0 ]]; then
echo ""
else
printf '%s' $1 | base64 -d
fi
}
e64 "Please-encode-me"
d64 "UGxlYXNlLWVuY29kZS1tZQ=="
#!/bin/bash
# Let's using of Bash Variables in Bash script, these are variables used by Bash that we can using them while of automation :)
# https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html
FILENAME=${BASH_SOURCE[0]}
BASHPATH=${BASH}
MAJOR=${BASH_VERSINFO[0]}
MINOR=${BASH_VERSINFO[1]}
PATCH=${BASH_VERSINFO[2]}
echo $FILENAME
echo $BASHPATH
echo $MAJOR.$MINOR.$PATCH
#!/bin/bash
# let's using chmod command in the Bash script. chmod help us to change files permission in Unix base system.
# we using this command to privilege some permission to users, groups, and other type
# base syntaxt :
# chmod ([u as user][g as group][o as other]) ([+ as add] or [- as remove]) ([w as write][r as read][x as executable]) [filename]
# let's add a user permission to execute a file
chmod u+x ./filename
# let's remove a user permission to execute a file
chmod u-x ./filename
# let's privileged user and group permission to execute a file
chmod ug+x ./filename
# let's remove privileged user and group permission to execute a file
chmod ug-x ./filename
# let's privileged user and group and other permission to execute a file
chmod ugo+x ./filename
# let's remove privileged user and group and other permission to execute a file
chmod ugo-x ./filename
# user permission read,write,execution
chmoe u+rwx ./filename
# user remove permission read,write,execution
chmoe u-rwx ./filename
# user permission read,write
chmoe u+rw ./filename
# user remove permission read,write
chmoe u-rw ./filename
# user and group permission read,write
chmoe ug+rw ./filename
# remove user and group permission read,write
chmoe ug-rw ./filename
# user and group and other permission read,write
chmoe ugo+rw ./filename
# remove user and group and other permission read,write
chmoe ugo-rw ./filename
#!/bin/bash
# Counting number of files inside a directory
# "find ." print list of files in current directory
# "wc -l" count number of files in this directories
find . | wc -l
#!/bin/sh
# Creat help menu with cat and function in bash script
print_help() {
cat <<EOF
TITLE HERE:
---------------------------------------
Simple description paragraph here,
And another lines
--------------------------------------
List of Command :
- this is one
- this is two
- this is three
EOF
}
print_help
#!/bin/bash
# Display file size with du
# -s option show apperent file size
# -h option show as humand readable
du -bh ./sample-file.jpg
#!/bin/bash
# dd command :
# is a linux useful command line tools that help us to read and write a binary file.
#
# we use it to read a binary file and export a part of it or (dump a part of it)
#
# Example :
# somtimes we encounter with a binary file that contained multiple file. how we could export a part of these files ? dd is our answer
#
# Real Example :
# If you did install cmake manually you should encounter with a cmake installer file like : cmake-3.26.3-linux-x86_64.sh
# cmake-3.26.3-linux-x86_64.sh is a binary file that countained multiple part.
# we check it with binwallk tool to recognize structure of it.
#
#
# DECIMAL HEXADECIMAL DESCRIPTION
# --------------------------------------------------------------------------------
#
# 0 0x0 Executable script, shebang: "/bin/sh"
# 577 0x241 Copyright string: "Copyright (c) Kitware""
# 2041 0x7F9 Copyright string: "Copyright 2000-2023 Kitware, Inc. and Contributors"
# 2313 0x909 Copyright string: "copyright"
# 2446 0x98E Copyright string: "copyright"
# 6628 0x19E4 Copyright string: "copyright and license notice applies to distributions of"
# 6813 0x1A9D Copyright string: "copyright notices"
# 9482 0x250A gzip compressed data, from Unix, last modified: 2023-04-04 19:10:59
#
# as you can see "gzip compressed data" contained all cmake files and we want dump it. we use dd command like this :
# if -> input file name
# bs -> block size to read
# skip -> skip from 0 to 9482 address and read from 9482 then write
# of -> output file name
dd if=cmake-3.26.3-linux-x86_64.sh bs=1 skip=9482 of=cmake-file.zip
#!/bin/bash
# Get current file name with ${0}
# Find the path with file name
# Create path
# return current file directory
# ${0} : return name of current file
# dirname return current file path
echo $(dirname ${0})
echo $(dirname "/usr/bin")
echo $(dirname "/usr/local/bin")
#!/bin/bash
# Find and check binary path
# To find path in bash script it is good to use [ command -v binfile ] command
# This command return path of binfile if this file is exist than return empty string
FUSE2FS_PATH=$(command -v fuse2fs)
RESIZE2FS_PATH=$(command -v resize2fs)
# Checking path with if statement and -z option
# -z option useful to check a empty string
if [ -z "$FUSE2FS_PATH" ]; then
# if command -v filename cant return file path we set correct path manualy
FUSE2FS_PATH=/usr/sbin/fuse2fs
fi
if [ -z "$RESIZE2FS_PATH" ]; then
# if command -v filename cant return file path we set correct path manualy
RESIZE2FS_PATH=/usr/sbin/resize2fs
fi
# Print path
echo $FUSE2FS_PATH
echo $RESIZE2FS_PATH
#!/bin/bash
# Using id command inside if statement to find user id
if [ $(id -u) != 0 ]; then
echo $(id -u)
fi
#!/bin/bash
# define a function in bash script
# $* = show all input args in a line
# $1 = select first arg
# $2 = display secound arg
# $3 = display third arg
# and so on...
die() {
echo "die: $*"
echo "name : $2"
echo "family : $3"
echo "email : $4"
exit 1
}
#call a function and send arguments to it
die "show me in die funciton" "name" "family" "0__0@example.com"
#!/bin/bash
# Get current file name inside bash script
# ${0} -> returned current file name
GET_CURRENT_FILE_NAME=${0}
echo $GET_CURRENT_FILE_NAME
#!/bin/bash
# nproc : /bin/nproc is really usefull, this littel boy command retrun number of process that current
# cpu on our machine can run. some time this command help us when we work with make command
#
# make :
# make can run multiprocess jobs, to running make with multi process task 'nproc' help us to
# running build process with make in multiprocess task : make -j $number_of_process
number_of_process=$(nproc)
echo "number of process that current machine support : $number_of_process"
#!/bin/bash
# lets use of the "cat" command to create and write to a file.
cat > filename.txt << 'EOF'
this is a text that will be write with cat command to the filename.txt
EOF
#!/bin/bash
# Get name of file as first argument that passed to bash script
# Edit and remove file extions with sed
# Get file name as input argument
FILE_NAME=$1
# Check file is exist with -e option inside if
if [[ -e "./$FILE_NAEM"]]; then
# Remove .asm from endof string with sed and replace it with ''
# Then store it inside FILENAME_WITHOUT_EXT
FILENAME_WITHOUT_EXT=$(sed '/s/.asm//' <<<"$FILE_NAME")
# Print file name without ext
echo $FILENAME_WITHOUT_EXT
fi
#!/bin/bash
# Get wget binary file path
WGET_PATH=$(command -v wget)
# Check WGET_PATH is executable
if [ -x "$WGET_PATH" ]; then
echo $WGET_PATH
fi
# Using "/dev/null" device and redirection ">" and -x options in if statement
# We use "-x" option to check a executable is exist
# Using "&&" to assurance binary file is exist
# Using 2>&1 help us to redirect any error on $WGET_PATH --help ro stream output
# Using "| grep prxoy" get proxy from stream output
# Using "> /dev/null" redirection stream output to /dev/null device. we now when redirect anyting to /dev/null
# It return EOF, when we need to avoid print stream out put redirect stream output to /dev/null
# Totally this command : $WGET_PATH --help 2>&1 | grep proxy > /dev/null
# Check proxy for output of wget --help then redirect out put to /dev/null
if [ -x "$WGET_PATH" ] && $WGET_PATH --help 2>&1 | grep proxy > /dev/null; then
echo $WGET_PATH
fi
# Check file is exist with "-f" option
TEXT_PATH=./text.txt
if [ -f "$TEXT_PATH" ]; then
echo "text.txt file is exist"
else
echo "text.txt file isn't exist"
fi
SCRIPT_DIR="$(dirname "${0}")"
echo $SCRIPT_DIR
echo ${0}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment