Posts

Showing posts with the label bash script

Linux bin bash shell

The most popular shell in Linux? It is Linux Bash shell. The path and executable of the Bash shell is /bin/bash. /bin/bash is a binary. Script File Interpreter Opensource and Linux script file are very important. In bash script use #! / bin/bash specifies which will be interpreted with the bash shell or interpreter /bin/bash #!/bin/bash Alternative Shells Bash shell is very popular shell but there are a lot of alternative shells. SH is very basic shell which provides fundamental features of the Bash. KSH is Korn shell which is similar to the BASH CSH is mainly designed from C programming language. Difference Between #!/bin/sh and #!/bin/bash Bash shell is stored in the /bin/bash path. /bin/sh provides the SH shell which is cirppeled down version of the BASH. Detailed information man bash or man /bin/bash

Split a string in shell and get the last field

Image
How to split a string in shell and get the last field. in this tutorial, i written a small program use bash script split string /www/huu/phan/com/xyz to new path /www/huu/phan/com and last field of new path /www/huu/phan/com is com . Introduction Input: one_path is /www/huu/phan/com/xyz Output: new_path is  /www/huu/phan/com last_field of new_path is com My bash shell as below: #!/bin/bash ############### # # Author: HuuPV # My blog: www.huuphan.com # ############### # How to split a string in shell and get the last field one_path="/www/huu/phan/com/xyz" last_slash=-1 for (( i=0; i < ${#one_path}; i++)); do         if [ ${one_path:$i:1} == '/' ]         then                 last_slash=$i;         fi done new_path=${one_path:0:$last_slash} last_field=$(basename $new_path) echo "Old path: $one_path" echo "New path: $new_path" echo "Last field of $new_path:  $last_field" The display as picture below:

Bash script arguments from a file content

Image
In this tutorial, how to change string content of file as arguments. I written  Bash script arguments from a file content as below: Input file How to change NAME and DOMAIN in input file with any arguments in content as below. My name: NAME My Blog: https:// DOMAIN .com Output file with arguments default value My name: Huu My Blog: https://huuphan.com Output file with arguments another vaule  My name: Devops My Blog: https://devopsroles.com My bash script  $ cat arguments_file.sh The content bash script #!/bin/bash argument1=Huu argument2=huuphan INPUT=/home/huupv/scripts/input.txt OUTPUT=/home/huupv/scripts/output.txt RED='\033[0;31m' NC='\033[0m' # No Color if [ $# -eq 2 ];then     argument1=$1     argument2=$2 fi sed -f - "$INPUT" > "$OUTPUT"<<EOF     s/NAME/$argument1/g     s/DOMAIN/$argument2/g EOF Running bash script with default value $ ./arguments_file.sh The output as picture below: To run

Bash script argument default value and takes optional input arguments

Image
In this tutorial, I have written bash script Bash script argument default value and takes optional input arguments. Using $# argument to input parameter  in bash script. To run bash script with argument default ./def_argument_default.sh The display argument default as below: Running bash script with argument   ./def_argument_default.sh argument1 argument2 argument3 The display bash script with argument as below: My bash script argument default value and takes optional input arguments as below: #!/bin/bash ############### # bash script argument default value # running argument default:  #    ./def_argument_default.sh  # Running optional input argument: #    ./def_argument_default.sh argument1 argument2 argument3 # ############### argument1=HUU argument2=PHAN argument3=www.huuphan.com RED='\033[0;31m' NC='\033[0m' # No Color if [ $# -eq 3 ];then     argument1=$1     argument2=$2     argument3=$3 fi echo -e "First Name: $RED \t$

Bash script Change folder Permissions Recursively

Image
In my post, How to use Bash script Change to folder Permissions Recursively. For example, To change all folder and file under huuphan.com folder. To list tree file and folder under huuphan.com folder [root@huupv huupv]# tree huuphan.com/ huuphan.com/ ├── a │   ├── abc.txt │   └── b │       └── c └── K 4 directories, 1 file To use find command list all file and folder under huuphan.com folder [root@huupv huupv]# find /home/huupv/huuphan.com -type d -exec ls -ld {} + >chown_folder.txt To use cat command read chown_folder.txt file [root@huupv huupv]# cat chown_folder.txt  The content in chown_folder.txt file file as below: drwxrwxr-x. 4 huupv huupv 4096 Mar  9 22:38 /home/huupv/huuphan.com drwxrwxr-x. 3 huupv huupv 4096 Mar  9 22:38 /home/huupv/huuphan.com/a drwxrwxr-x. 3 huupv huupv 4096 Mar  9 22:37 /home/huupv/huuphan.com/a/b drwxrwxr-x. 2 huupv huupv 4096 Mar  9 22:37 /home/huupv/huuphan.com/a/b/c drwxrwxr-x. 2 huupv huupv 4096 Mar  9 22:38 /home

reverse dns lookup script

Image
How to use bash script check check reverse dns lookup. You can check online reverse dns lookup with https://mxtoolbox.com/ or http://www.dnsqueries.com/en/reverse_lookup.php , so forth. Reverse dns lookup full script     #!/bin/bash     # Author: HuuPV     # MTA Reverse DNS lookup:     # For MTA     # dig mydomain.com +short @8.8.8.8     # dig -x 111.222.121.221 +short @8.8.8.8     rm -f /tmp/reverse_lookup_MTA     IP1="111.222.121.221"     MTA="mydomain.com"     DIG1=$(dig $MTA +short @8.8.8.8)     PTR1=$(dig -x $DIG1 +short @8.8.8.8 | sed 's/.$//')     #To check MTA DNS lookup status     echo "##### MTA Reverse DNS lookup and PTR Query #####" >/tmp/reverse_lookup_MTA     if [ "$MTA" != "$PTR1" ]; then         echo "$MTA != $PTR1" >>/tmp/reverse_lookup_MTA         echo "Reverse lookup Failed!" >>/tmp/reverse_lookup_MTA     elif [ "$IP1" != "$DIG1" ]; then         echo

Bash script while loop case statement example

Image
How to create "Menu Lists" use Bash script while loop case statement. I'm use while loop with case statement create "Menu Lists". Bash script for fun! The display color for echo command Red color RED='\033[0;31m'   Blue color BLUE='\033[0;34m' No color NC='\033[0m' Read from standard input and write to standard output and files tee -a /tmp/log__$DATE  To run bash script [huupv@huupv ~]$ ./menu_list  The display as below:  Menu Lists  1) My blog  2) Your name  3) How old are you?  4) Your Hobbies  5) Quit >> My full bash script "bash script while loop case statement" example #!/bin/bash # author: HuuPV RED='\033[0;31m' BLUE='\033[0;34m' NC='\033[0m' #No color DATE=$(date +%Y%m%d) while true do     echo ""     echo -e "$BLUE Menu Lists $NC"     echo -e ""     echo -e "$BLUE 1) My blog $NC"     echo -e "$BLUE 2)

conditional expression bash script

Image
Conditional expression in bash script.For example, check file exists with Conditional expression Bash script checkfile.sh  [huupv@huupv scripts]$ cat checkfile.sh  The content as below: #!/bin/bash # author: HuuPV # for example basic for check file exists! FILE="/home/huupv/scripts/menu_list" if [ -f $FILE ]; then     echo -e "$FILE \t File exists!" else     echo -e "$FILE \t File not exists!" fi File operators: -a FILE        True if file exists. -b FILE        True if file is block special. -c FILE        True if file is character special. -d FILE        True if file is a directory. -e FILE        True if file exists. -f FILE        True if file exists and is a regular file. -g FILE        True if file is set-group-id. -h FILE        True if file is a symbolic link. -L FILE        True if file is a symbolic link. -k FILE        True if file has its `sticky' bit set. -p FILE        True if file is a named pipe. -r FILE       

Bash script allign the output

Image
How to use bash script allign the output. To use printf command the format of strings. Many method to allign the ouput use bash script. The script my below simple. Bash script align the output #!/bin/bash for b in "H HU HUU PHAN PHANVAN" do         echo "$b"         for c in $b         do                 printf "%-50sOK\n" "$c"         done done Running bash script [huupv@huupv Downloads]$ ./studyscript.sh  The display as below

bash script use IFS in context of for looping

Image
How to use bash script IFS in context of for looping. what does IFS meaning? The IFS stands for "internal Field separator", That is used how to do word splitting. IFS is a special shell variable. The used for looping in context of file. Bash script use IFS in context of for looping #!/bin/bash #Author: HuuPV IFSOLD=$IFS IFS=$'\n':,' ' for entry in "huu:phan linux operating,system" do     echo "Values in $entry -" #IFS=': ,'     for value in $entry     do         echo " $value"     done done IFS=$IFSOLD Running bash script use IFS in context of for looping [huupv@huupv Downloads]$ ./IFS_test.sh The display as below: Values in huu:phan linux operating,system -  huu  phan  linux  operating  system

Bash script list all IP addresses connected to Server

Image
Running to bash script list all IP addresses connected to Server ./studyscript.sh  >/dev/null 2>&1 The bash script list all IP addresses connected to Server #!/bin/bash rm -f /tmp/list_IP rm -f /tmp/list_IPP rm -f /tmp/list_IPdone netstat -ntu | grep ESTA | awk -F: '{ print $2 }' | sed -r 's/^.{12}//' |sed 's/:/\n/g' | sed '/^\s*$/d'| sort > /tmp/list_IP #sleep 10 while read -r p do  curl ipinfo.io/$p done < /tmp/list_IP >/tmp/list_IPP cat /tmp/list_IPP | egrep "ip|country" |sed '$!N;s/\n/ /' |sed -r 's/^.{9}//' |sed 's/"country": "//' |sed 's/",//g' >> /tmp/list_IPdone echo "-----IP------  --country--" >> /tmp/list_IPdone Note the bash script list all IP addresses connected to Server sed 's/:/\n/g' Delete duplicate lines sed '/^\s*$/d' Delete empty lines sed '$!N;s/\n/ /' How to merge every two lines in

bash script check file and directory

Image
How to check file and directory in directory, I use bash script if file exists. To write simple bash script check folder /home/huupv/Desktop all file and directory. The first, use for loop /home/huupv/Desktop/* The secondary, to check if a directory then echo, elif a file then echo. My bash scripts check file and directory     #!/bin/bash     # Author: HuuPV     for file in /home/huupv/Desktop/*     do         if [ -d "$file" ]         then             echo "$file is directory"         elif [ -f "$file" ]         then             echo "$file is file"         fi     done The running bash script check file and directory $ ./studyscript.sh  The output as bellow /home/huupv/Desktop/Barbarpower Template.xml is file /home/huupv/Desktop/HuuPV is directory /home/huupv/Desktop/like_fb.sh is file /home/huupv/Desktop/post-fb.docx is file /home/huupv/Desktop/Untitled Document 1 is file