Advanced Bash-shell Guide (Version 10) study notes three

Source: Internet
Author: User

The script on the book compares the more useful scripts for more records

A better way to check if the command-line arguments are numbers

e_wrongargs=85 # non-numerical argument (Bad argument format). The #42 # case "$" in43 # "") lines=50; # *[!0-9]*) echo "Usage: ' basename ' Lines-to-cleanup"; exit $E _wrongargs; # *) lines=$1;; # ESAC


Better way to check if the number of command-line arguments is correct

1 e_wrong_args=852 script_parameters= "-a-h-m-z" 3 #-A = all,-h = Help, etc.45 if [$#-ne $Number _of_expected_args]6 Then7 echo "Usage: ' basename $ ' $script _parameters" 8 # ' basename $ ' is the script ' filename.9 exit $E _wrong_args10 fi


Better way to check if the correct directory

# Cd/var/log | | {# echo "Cannot change to necessary directory." >&265 # Exit $E _xcd;66 #}


Back up the source directory files and unzip them in the target directory

(cd/source/directory && Tar CF-.) | (Cd/dest/directory && Tar xpvf-) A more efficient script is the CD source/directory# Tar CF-. | (CD..) /dest/directory; Tar xpvf-) or cp-a/source/directory/*/dest/directory# cp-a/source/directory/*/source/directory/. [^.] */dest/directory #这个复制源目录的隐藏文件


Back up files that changed over the last 24 hours

#!/bin/bashbackupfile=backup-$ (date +%m-%d-%y) archive=${1:-$BACKUPFILE}# If no parameters are specified on the command line, it is in the following format # It would default to " Backup-mm-dd-yyyy.tar.gz. " Tar cvf-' find. -mtime-1-type f-print ' > $archive. Targzip $archive. Tarecho "Directory $PWD backed up in archive file \" $archive. Tar. Gz\ "."


If the file is too many or the file name has a white-space character, the above script may be wrong

Better backup solution tar-r append to archive file

#-------------------------------------------------------------------# Find. -mtime-1-type f-print0 | Xargs-0 tar rvf "$archive. Tar" or # Find. -mtime-1-type f-exec tar rvf "$archive. Tar" ' {} ' \;exit 0


Get the last parameter of a command-line argument

args=$# # Number of args passed.lastarg=${!args}# note:this is a *indirect reference* to $args ... # or:lastarg=${!#}


${file#*/}: Take out the first/its left string: dir1/dir2/dir3/my.file.txt
${file##*/}: Take out the last/and left string: my.file.txt
${file#*.} : Take out the first one. And the string to the left: file.txt
${file##*.} : Take out the last one. And the string to the left: txt
${file%/*}: Take off the last bar/its right string:/dir1/dir2/dir3
${file%%/*}: Remove the first/its right string: (null value)
${FILE%.*}: Take off the last one. And the string to the right:/dir1/dir2/dir3/my.file
${FILE%%.*}: Take out the first one. And the string to the right:/dir1/dir2/dir3/my




This article is from the "Linux is belong to You" blog, make sure to keep this source http://jwh5566.blog.51cto.com/7394620/1657455

Advanced Bash-shell Guide (Version 10) study notes three

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.