"Shell Script Basics"
Create a script
#vim 1.sh
*********************
#! /bin/bash
echo "Hello world!"
*********************
#chmod +x 1.sh
#sh 1.sh
Hello world!
< weak reference "": Place the string in double quotes, preserving the literal value of all characters in the string, except for the $, ', \, and! characters. In other words, the variable
Extensions and command extensions still work within double quotation marks.
#echo "Can I have a $PATH"
Can I have A/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/home/kiosk/.local/bin:/home/kiosk/bin
#echo "The current time is $ (date +%r)."
< strong quote: Place the string in single quotation marks, preserving the literal value of all characters in the string, and disabling all disables all extensions:
#echo ' ab$$$ '
ab$$$
< escape \, #, ';: non-referenced \ is an escape character. It retains the literal value of the next character. (for example, \ $PATH is the exact string $path, not the contents of the PATH variable.) )
#echo make \$\$\$
Make $$$
#echo make $$$
3006$
#echo #comment #
#echo ' pwd '
/root/
Shell variables
The shell variable is used to specify a value for the name that is later used in the script, and is limited to the shell command line or from declaring the variable
The volume of the script.
#cd/etc/skel//This directory has some user-defined variable profiles saved. Bash_profile. BASHRC
#env//List all system variables set by the system domain
HOSTNAME, SHELL, Histsize, MAIL, PATH, PWD, LANG, HOME, logname and other environment variables
#set//can also output environment variables that can display user-defined variables.
(1) Allow all users in the system to log in and use the amount of change. To do this, add a row at the end of the/etc/profile file to export Myname=chengcheng save exit. #source/etc/profile
#echo $myname
Chengcheng
#su Student
#echo $myname
Chengcheng
: OK indicates the configuration was successful.
(2) Only the current user is allowed to use the change amount. How to do this: in the current user's home directory, the last line of the. bashrc file is added to export mywork=linux, and then #source. BASHRC is in effect.
(3) Change the Umask value to change the contents of two files/ETC/PROFILE,/ETC/BASHRC
Substitution of commands (variable assignment)
#echo $ (ls-l) = = ' Ls-l '
@@@ 练习题, copy and name the file in the/etc directory ending with. conf. time
#find/etc/-type f-name ' *.conf '-exec cp {}/mnt \;
#ls |xargs-n1-i{} MV {}/mnt/{}.$ (date +%t)
Use the script as follows:
#! /bin/bash
For name in ' find/etc/-name "*.conf" '
Do
Cp-r $name/mnt/' basename $name '. ' Data +%t '
Done
"Arithmetic operator"
++,--,+,-,/,%,*,**,+=,-=
Use the shell to calculate the sum of 1 to 100:
#seq 1 |xargs |sed ' s//+/g ' |BC
5050
#!/bin/bash
J=0
For i in ' seq 1 '//' seq 1 ' = = $ (seq 1 100) = = {1..100}
Do
j=$[$j + $i] = = ((j+=i))
Done
Echo $j
**********************************
#!/bin/bash
For ((i=1;i<=100;i++))
Do
((j+=i)) ==j= ' axpr $i + $j ' ==j=$[j+=i]
Done
Echo $j
#let a=2+9
#echo $a
11
#echo ' axpr 3+4 '
7
Loop for
for ((i=0; i<=100; i++));d o j= ' expr $j + $i ';d one;echo $j
"Database Backup" MySQL is now being acquired by Oracle to launch his Oracle, and now many companies are using MySQL because it is open source free cost savings, and like the Bank and enterprise use of Oracle, Red Hat has developed a MySQL-compatible MARIADB database with the source code of the source MySQL.
Installation of the database
#yum install-y mariadb Mariadb-server
#mysql-E "show databases;"-e-n |grep-v ' ^* ' |grep-v ' schema$ '
#mysqldump-uroot-p db > Db.sql
#mysql-uroot-p DB < Db.sql
Back up only one table #mysqldump-uroot-p db table > Dbtable.sql
Specifying a character set at backup #mysqldump-uroot-p--default-character-set=utf8 db> 1.sql
#mysql-uroot-p--default-character-set=utf8 db< 1.sql
"Script automatically backs up the database"
#!/bin/bash
For DB in $ (mysql-e "show databases;"-e-n | grep-v ' ^* ' | grep-v ' schema$ ')
Do
echo "Backing Up $DB"
Mysqldump $DB >/dbbackup/$DB. Dump
Done
echo ""
For DBDUMP in/dbbackup/*
Do
size=$ (stat--printf "%s\n" $DBDUMP)
echo "$DBDUMP
$SIZE "
Done
"Interactive Conditional judgment"
Script 1
#!/bin/bash
Read-p "Please input your username:" username
Useradd $username
Read-p "Please input your password:" Password
echo "$password" | passwd--stdin $username >/dev/null
Echo OK
Script 2: Output banana When the script is executed with Apple when the parameter is banana, when the apple
#!/bin/bash
If ["$*" = "Apple"];then
echo "Banana"
elif ["$*" = "Banana"];then
echo "Apple"
Else
echo "Sorry" Input with Apple | Banana
Fi
#! /bin/bash
Case $ in
Apple
echo "Banana"
;;
Banana
echo "Apple"
;;
*)
;;
Esac
Script 3,ping 10 IP
#!/bin/bash
For IP in ' SEQ 1 10 '
Do
Ping-w1-c1 172.25.254. $ip &>/dev/null
If ["$?"-eq "0"]; then
echo "Ping 172.25.254. $ip is Up"
Else
echo "Ping 172.25.254. $ip is Down"
Fi
Done
Numeric comparison operators:-eq,-ne,-lt,-le,-GT,-ge
string comparison operators: =,! =
#[ABC = ABC];echo $?
1
#[ABC = ABC];echo $?
0
"Expect script"
Test condition judgment
The test command can be used to evaluate an expression in a bash script. It evaluates the expression specified by its argument, if the expression
True to return a 0 exit state, or a non-0 exit state if the expression is false. Test has an alternate language
Use square brackets "[]" to enclose the expression so that it is easier to read.
1) Judgment expression
if test (expression is true)
if test! An expression is false
Test expression 1–a expression 22 expressions are true
Test expression 1–o Expression 22 expressions have a true
Test expression 1! Expression 2 Conditional negation
2) Judging the string
Test–n string string is not zero in length
Test–z whether the length of the string string is zero
Test string 1 = string 2 string is equal, if equal returns True
Test string 1! = String 2 If the string is unequal, false if not
3) Judging integers
Test integer 1–eq integer 2 integer equal
Test integer 1–ge integer 2 integer 1 greater than or equal to integer 2
Test integer 1–gt integer 2 integer 1 greater than integer 2
Test integer 1–le integer 2 integer 1 less than or equal to integer 2
Test integer 1–lt integer 2 integer 1 less than integer 2
Test integer 1–ne integer 2 integer 1 not equal to Integer 2
4) Judgment document
Test File1–ef File2 Two files are the same file and can be used for hard connections. The main decision is whether two files point to the same inode.
Test File1–nt File2 Determine if file 1 is newer than file 2
Test File1–ot File2 Judge the file 1 than whether the file 2 old
Test–b file file whether block device files
Test–c file file and is character device files
Test–d file files and is a directory
Test–e file files are present (common)
Test–f file files are regular files (common)
Test–g file is set with group ID
Test–g The valid group ID that the file belongs to
Test–h file is a symbolic link (same-l)
Test–k file is set sticky bit bit
Test–b file files exist and are block device files
Test–l file is a symbolic link (same-h)
Test–o file is a valid user ID
Test–p file is a named pipe
Test–r file is readable
Test–s file file is non-blank
Test–t FD file descriptor is opened in a terminal
Test–u file exists and its Set-user-id bit is set
Test–w file is present and writable
Test–x file files are non-existent and executable
Alias alias Command
#alias hah= "LS"
#unalias hah
Using functions
This article is from the "Love You in Time" blog, please be sure to keep this source http://solongcc.blog.51cto.com/8979378/1630474
Shell Basics Exercises