Day6 - understanding and learning BASH

Day6 - understanding and learning BASH

0X01 BASH Foundation

1. shell built-in properties

  1. Gets the length of the string

    length=${#Var} can get the length of var
    
  2. Gets the shell currently in use

    $BASH $0
    
  3. Check current running user

    if [ $UID -ne 0 ] then
    	echo NOT ROOT USER. Please run as root
    else
    	echo Root User
    fi
    

    perhaps

    if test $UID -ne 0 then
    	echo NOT ROOT USER. Please run as root
    else
    	echo ROOT USER
    fi
    
  4. Modify the prompt character of bash

    Environment variable: PS1 can control

    cat ~/.bashrc | grep PS1
    

    There are also some special characters that can be used in it

    \u Expand to user name
    \h Expand to host name
    \w Expand to schedule directory
    

2. Add environment variable to function

prepend() { [ -d "$2" ] && eval $1=\"$2':'\$$1'\" && export $1;}

3. Array

echo ${array[*]}
echo ${array[@]}

Print array elements as characters, respectively

4. Terminal operation

stty -echo takes input as a password but not output

#!/bin/bash
echo -e "Please Enter password:"
stty -echo
read password
stty echo
echo 
echo password read.

Design a terminal countdown

#!/bin/bash
echo Count;
tput sc
for count in `seq 0 40`
do
	tput rc
	tput ed
	echo -n $count
	sleep 1
done

5. Debugging script

set -x Display parameters and commands when executing
set +x Prohibit debugging
set -v Displays the input when the command reads
set +v Print input prohibited

6. Get input

read 
-n Specified quantity
-s No echo
-p Prompt information
-t time limit
-d Specific separator

7. Profile customization bash

Login shell: / etc / Profile Profile / Bash_ login

Interactive shell and ssh single execution: bash bashrc . bashrc

BASH_ENV: alias can only be used in sub shell after setting

Configuration file read by ssh login

1. /etc/profile
2. /etc/bash.bashrc
3. profile

0x02 command execution

1. Record and play back the terminal session

script -t 2> timing.log -a output.session

scriptreplay; Follow up the implementation process.

2.xargs

Use in combination with find. Pay attention to specifying parameters

find xxx -print0 | xargs -0 

3. md5sum

  • To file md5
md5sum file > a.md5 generate
md5sum -c a.md5  fast check
  • md5 for directories
md5deep -r1 dir > a.md5

-r specifies the iteration depth, which is one layer here

  • Password hash

    openssl password -1 -salt salt_string password
    

4. Sorting

sort data.txt | uniq -s 2 -w 2
  • sort

    -nrk Sort in reverse order according to the first column
    -k 2 Sort by column 2
    -d Sort by dictionary
    
  • uniq

    -c Number of occurrences of Statistics
    -n Show unique rows
    -d Show duplicate
    -s skip
    -w How much from now
    

5. Segmentation

split is useful for writing data in a non offline environment.

-b Specify individual file sizes
-d Use numeric suffixes
-a Suffix length
-l Specify the number of rows

csplit

{Real number} Specifies the number of times the split is executed
-s Command silence
-n File name suffix number format
-f Prefix of suffix
-b Specify suffix format

6. Parallel

Parallel computing yyds is an open sub shell

#!/bin/bash
PIDARRAY=()
for file in File1.iso File2.iso
do
	md5sum $file &
	PIDARRAY+=("$!")
done
wait ${PIDARRAY[@]}

7. Tree view

cd /var/log
find . -exec sh -c 'echo -n {}| tr -d "[:alnum:]_.\-" | tr "/" " "; basename {} ' \;

0x03 file operation

  • **-**As a command line parameter, this enables you to get input from stdin

1. Create files of any size

dd if=/dev/zero of=junk.data bs=1m count=1

if: input file

of: output file

bs: specify unit size

count: number of blocks copied

2. Delete the same file

#!/bin/bash
# name: remove_duplicates.sh
# use: remove the same
ls -lS --time-style=long-iso | awk 'BEGIN{
	getline; getline;
	name1=$8; size=$5
}
{
	name2=$8;
	if(size==$5)
{
	"md5sum "name1 | getline; csum=$1;
	"md5sum "name2 | getline; csum2=$1;
	if( csum==csum2 )
	{
		print name1; print name2;
	}
};
size=$5; name1=name2;
}' | sort -u > duplicate_files

cat duplicate_files | xargs -I {} md5sum {} | \
sort | uniq -w 32 | awk '{ print $2 }' | \
sort -u > unique_files

echo Remving....
comm duplicate_files unique_files -3 | tee /dev/stderr | \
	xargs rm

echo Removed duplicates files successfully.

3. File permissions

  1. Sticky bit of directory

    If the directory has this permission set, only the creator can delete it
    /tmp
    
  2. Recursive weighting

    -R

  3. Make the file non modifiable

    Chatr sets the extended properties of the file

    chattr +i file
    chattr -i file
    
  4. View file status

    !/bin/bash
    # name: filestat.sh
    if [ $# -ne 1 ];
    then
    	echo "Usage: is $0 basepath";
    	exit
    fi
    path=$1
    
    declare -A statarray;
    
    while read line;
    do
    	ftype=`file -b "$line" | cut -d, -f1`
    	let statarray["$ftype"]++;
    done < <(find $path -type f -print) #Note that this is not < < the first < is redirection, and the second is sub process output conversion
    
    echo ============ FIle types and counts =============
    for ftype in "${!statarray[@]}";
    do
    	echo $ftype: ${statarray["$ftype"]}
    done
    
    
  5. Create 1g ext4 files and mount

    dd if=/dev/zero of=look.img bs=1G count=1
    mkfs.ext4 look.img
    file look.img
    mkdir /mnt/loop
    mout -o loop look.img /mnt/loop`	
    

    -o loop refers to the loopback i file mounted by name, not the device

    fdisk standard partitioning tool+

     losetup -o 32256 /dev/loop2 look.img
    

    If you need to create a partition, you must manually wipe it

    losetup /dev/loop1 loo.img
    fidsk /dev/loop1
    

    In loop Create a partition in img and mount the first partition

    losetup -o 32256 /dev/loop2 loopback.img
    
  6. Mount ISO files

In this way, when we access the directory, we access the data on the iso file.

mkdir /a
mount -o loop linux.iso /a

4. iso documents

Create iso file

dd if=/dev/cdrom of=iamge.iso
#At this time, all files will be imported into the corresponding directory
mkisofs -v "Label" -o image.iso dir/

-v: Specifies the of the volume label.

Create a mixed iso file of boot flash and hard disk

isohybrid image.iso
dd if=image.iso of=/dev/sdb1

Burn iso with command line

cdrecourd -v dev=/dev/cdrom image.iso -speed 8

Eject tray

eject
eject -t Close.

5. Document differences

Directory differences

diff -Naur dir1/ dir2/
-N Treat missing files as empty
-a Treat all files as text files
-u Generate integrated output
-r All files in recursive directory

6. Monitor file changes

dmeg | tail -f

7. Audio files

*Ubutu Studio
Subject matter.

Create a mixed iso file of boot flash and hard disk

isohybrid image.iso
dd if=image.iso of=/dev/sdb1

Burn iso with command line

cdrecourd -v dev=/dev/cdrom image.iso -speed 8

Eject tray

eject
eject -t Close.

5. Document differences

[external chain picture transferring... (img-61M4cKxf-1640655330273)]

Directory differences

diff -Naur dir1/ dir2/
-N Treat missing files as empty
-a Treat all files as text files
-u Generate integrated output
-r All files in recursive directory

6. Monitor file changes

dmeg | tail -f

7. Audio files

*Ubutu Studio

Keywords: Linux bash

Added by Bunkermaster on Mon, 03 Jan 2022 18:52:38 +0200