Advanced Bash-shell Guide (Version 10) Learn note one

Source: Internet
Author: User


I like a word
The only-to really learn scripting are to write scripts
The only way to learn a script is to write a script

Better command-line parameter detection
e_wrongargs=85 # non-numerical argument (Bad argument format).
41 #
# case "$" in
# "") lines=50;;
# *[!0-9]*) echo "Usage: ' basename $ ' lines-to-cleanup";
No number in the middle of the argument, invalid argument
# exit $E _wrongargs;;
# *) lines=$1;;
# ESAC

More Efficient directory detection
3 # Cd/var/log | | {
# echo "Cannot change to necessary directory." >&2
# # exit $E _xcd;
66 #}

#!/bin/sh invokes the default shell interpreter, which
Defaults To/bin/bash on a Linux machine.
#!/bin/bash is called Sha-bang magic number.

Whether the number of parameters for the test call is correct
1 e_wrong_args=85
2 script_parameters= "-a-h-m-z"
3 #-A = all,-h = help, etc.
4
5 if [$#-ne $Number _of_expected_args]
6 Then
7 echo "Usage: ' basename ' $script _parameters"
8 # ' basename ' is the script's filename.
9 Exit $E _wrong_args
Ten fi

Invoke script
Bash ScriptName

chmod 555 ScriptName (gives everyone read/execute permission) [9]
Or
chmod +rx scriptname (gives everyone read/execute permission)
chmod U+rx ScriptName (gives only the script owner Read/execute permission)

./scriptname

1 #!/bin/rm
2 # self-deleting script.
echo "This line would never print (betcha!)."
This is a suicide script.
# Nothing much seems-happen when you run the This ...
Except that the file disappears.

Change the document to #!/bin/more and add executable permissions
The result is a self-listing document similar to Cat XX | More


Special characters
# comment, line start # comment, not executed, cancel syntax detection
Inside the quote and escape characters are not comments

6 echo ${path#*:} # Parameter substitution, not a comment.
7 echo $ ((2#101011)) # Base conversion, not a comment.

; Command delimiter
1 echo Hello; echo there
;; Case Option Delimiter
1 case ' $variable ' in
2 ABC) echo "\ $variable = abc";;

. Equivalent to source refresh configuration file, reload

As part of the file name, hide the file

Equivalent to the current directory,.. Equivalent to the parent directory

Match a single character in a regular expression

"Partial reference or weak reference, suppress most of the special characters
' Strong references, suppress all special characters
, linking a sequence of arithmetic operations, returning only the final result
1 let "t2 = ((a = 9, 15/3))"
2 # Set "a = 9" and "t2 = 15/3"

Link string
1 for file in/{,usr/}bin/*calc
2 # ^ Find all executable files ending in "Calc"
3 #+ in/bin and/usr/bin directories.

\ escape character, meaning of expression character literal
/file path delimiter
' Output command result to variable
: Do nothing, placeholder
:> change the file length to 0 without changing the permissions, creating
: > Data.xxx # File "Data.xxx" now empty.
Same effect as Cat/dev/null >data.xxx
Also available as a domain delimiter in/etcpasswd

! Transition exit status or sense of test
Change the sense of equal (=) to not-equal (! =)
Command history can also be invoked

* General distribution
Match 0 or more characters in a regular
In arithmetic, a single representation of multiplication sign two represents factorial

? In the double brackets,? As a ternary operator
((var0 = var1<98?9:21))
Represents a single character in a wildcard and regular

$ variable
The value of the variable is represented by the variable name
Represents the end of a line in a regular
[email protected] $* position parameter
$? Variables for exit status
$$ indicates the process ID of the current script
() command group, the command in parentheses is a child shell, not visible to the outside
Array initialization
1 array= (element1 element2 element3)
{} command extension
8 CP File22. {Txt,backup}
9 # Copies "File22.txt" to "File22.backup"
----
echo {file1,file2}\: {\ A, "B", ' C '}
File1:a file1:b file1:c file2:a file2:b
File2:c
----
echo {A.. Z} # A b c d e F g h i j k l m n o p q R S t u v w x y Z
----
8 base64_charset= ({A). Z} {A.. Z} {0..9} +/=)
9 # Initializing An array, using extended brace expansion.
Represents a block of code
Visible to other parts of the script, non-child shell
1 a=123
2 {a=321;}
3 echo "A = $a" # a = 321 (value inside code block)
Also available as a placeholder
Ls. | Xargs-i-t CP./{} $

[] indicates the test
Array elements
1 array[1]=slot_1
2 echo ${array[1]}
Represents a range of characters
$ (()) integer expression
1 a=3
2 b=7
3
4 echo $ (($a + $b)) # 10
> &> >& >> < <> redirect Operators
< > as ASCII code comparison
\<,\> Word anchoring
| Pipeline
Passes the output (stdout) of a previous command to the input (stdin) of the next one
The pipe runs as a child shell, so you cannot modify the variables of the parent shell
>| Force redirection
|| Logical OR
& Background Run Job
&& Logic and
^ Line Head match

Back up files modified in the current directory in the last 24 hours
1 #!/bin/bash
2
3 # Backs up all files in current directory modified within last hours
4 #+ in a "tarball" (tarred and gzipped file).
5
6 backupfile=backup-$ (date +%m-%d-%y)
7 # embeds date in backup filename.
8 # Thanks, Joshua Tschida, for the idea.
9 archive=${1:-$BACKUPFILE}
# If no backup-archive filename specified on command-line,
#+ it would default to "backup-mm-dd-yyyy.tar.gz."
12
Tar cvf-' find. -mtime-1-type f-print ' > $archive. Tar
Gzip $archive. Tar
echo "Directory $PWD backed up in archive file \" $archive. tar.gz\ "."
16
17
# Stephane Chazelas points out that the above code would fail
#+ If there is too many files found
#+ or if any filenames contain blank characters.
21st
# He suggests the following alternatives:
#-------------------------------------------------------------------
# Find. -mtime-1-type f-print0 | Xargs-0 tar rvf "$archive. Tar"
Using the GNU version of "Find".
26
27
# Find. -mtime-1-type f-exec tar rvf "$archive. Tar" ' {} ' \;
# Portable to other UNIX flavors, but much slower.
#-------------------------------------------------------------------
31
32
Exit 0

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

Advanced Bash-shell Guide (Version 10) Learn note one

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.