1. Write a script that calculates the factorial of 10 using a loop #!/bin/sh
Factorial=1
For a in ' SEQ 1 10 '
Do
factorial= ' expr $factorial \* $a '
Done
echo "10! = $factorial "
2. Write a script, after the execution, print a line prompt "Please input a number:", ask the user to enter a value, then print out the value, and then ask the user to enter a value again. Until the user enters "end" to stop. #!/bin/sh
unset var
While ["$var"! = "End"]
Do
Echo-n "Please input a number:"
Read Var
If ["$var" = "End"]
Then
Break
Fi
Echo "Var is $var"
Done
//////////////////////////////////////////
#!/bin/sh
unset var
While ["$var"! = "End"]
Do
Echo-n "Please input a number:"
Read Var
If ["$var" = "End"]
Then
Break
Fi
Echo "Var is $var"
Done
3. Write a script that uses loops and continue keywords to calculate the sum of the numbers within 100 that can be divisible by 3
4. A function that calculates the product of all parameters using shift, assuming that the parameters are all integers
#! /bin/sh
Result=1
While [$#-GT 0]
Do
result= ' expr $result * $ '
Shift
Done
Echo $result
5. Write a script that can extract the tar.gz or tar.bz2 file by calling tar with the correct parameters, based on the parameter file name. #!/bin/shcase ${1##*.tar.} in bz2) tar jxvf $;; GZ) tar zxvf $;; *) echo "wrong file type" ESAC6. Write a script to make it easier for users to find information about RPM. This script first prompts the user to select the query basis, such as file name, package name, all. The user is then prompted to select the query information, such as the package name, all the files contained in the package, the package information, and so on. Then ask whether to continue the query, is to loop just the process, otherwise exit.
#!/bin/bash
# favourite OS. Samli 2004.4.19
echo "What's your favourite OS?"
Select Var in "Linux", "UNIX" "Windows" "other"; Do
echo "You have selected $var."
#break
Done
#!/bin/bash
# List A content summary of a number of RPM packages Samli 2004.4.19
# usage:showrpm Rpmfile1 rpmfile2 ...
# example:showrpm/cdrom/thizlinux/rpms/*.rpm
For Rpmpackage in $*;
Do
If [-R "$rpmpackage"];
Then
echo "=============== $rpmpackage =============="
/BIN/RPM-QIP $rpmpackage
Else
echo "Error:cannot Read file $rpmpackage"
Fi
Done
#!/bin/bash
If [$#-le 0]
Then
echo "Not Enough parameters"
Exit 1
Fi
#string = "2 3 4 5 6"
#set string
Sum=0
While [$#-GT 0]
Do
sum= ' expr $sum + $ '
Shift
Done
Echo $sum
#! /bin/bash
echo "*******************************"
echo "Please select your operation:"
echo "1 Copy"
echo "2 Delete"
echo "3 Backup"
echo "*******************************"
Read op
Case $op in
C
echo "Your selection is Copy"
;;
D
echo "Your selection is Delete"
;;
B
echo "Your selection is Backup"
;;
*)
echo "Invalid Selection"
Esac
#! /bin/sh
While True
Do
echo "*******************************"
echo "Please select your operation:"
echo "1 Copy"
echo "2 Delete"
echo "3 Backup"
echo "4 Quit"
echo "*******************************"
Read op
Case $op in
C
echo "Your selection is Copy"
;;
D
echo "Your selection is Delete"
;;
B
echo "Your selection is Backup"
;;
Q)
echo "Exit ..."
Break
;;
*)
echo "Invalid Selection,please try Again"
Esac
Done
#! /bin/sh
a=10
B=20
C=30
value1= ' expr $a + $b + $c '
echo "The value of value1 is $value 1"
value2= ' expr $c/$b '
echo "The value of value2 is $value 2"
value3= ' expr $c * $b '
echo "The value of Value3 is $value 3"
value4= ' expr $a + $c/$b '
echo "The value of Value4 is $value 4"
var4= ' expr $value 4-$value 2 '
Echo $var 4
#! /bin/sh
Sum=0
For I in $*
Do
sum= ' expr $sum + $i '
Done
Echo $sum
Abc=123
Echo $ABC
# # #定义函数
Example1 ()
{
abc=456
}
# # #调用函数
Example1
Echo $ABC
abc=234234
Example1
Echo $ABC
# # #定义函数, using Parameters
Example2 ()
{
echo $
Echo
}
# # #调用函数, passing parameters to it
EXAMPLE2 ABC BBB
Example2 DKSDFSDFSFAA BBB
#!/bin/bash
echo "Please input a file name:"
Read file_name
If [-D $file _name]
Then
echo "$file _name is a directory"
Elif [-F $file _name]
Then
echo "$file _name is a regular file"
elif [-C $file _name-o-b $file _name]
Then
echo "$file _name is a device file"
Else
echo "$file _name is an unkonwn file"
Fi
#! /bin/sh
Read First Second third
echo "The first parameter is $first"
echo "The second parameter is $second"
echo "The third parameter is $third"
#! /bin/sh
If [$#-ne 2]; Then
echo "Not Enough parameters"
Exit 0
Fi
if [$1-eq]; Then
echo "Equals $"
elif [$1-LT]; Then
echo "Less than"
elif [$1-GT]; Then
echo "$ greater than"
Fi
#clear
#:trap "" 2 18 20 24
#e Cho "***********************************************************************"
#e Cho "hi! You changed my root password "
#e Cho "This was a little punishment for you so you can use the shell"
#e Cho "must answer my three questions and type" yes "or" no "to answer"
#
#answer = "No"
#
#e Cho "***********************************************************************"
#e Cho "Please press" Enter "key to continue"
#read
#while [$answer = "No"]
#do
# echo
# echo "1.Are a boy? (yes/no) "
# Read Answer
#done
#
#answer = "No"
#
#until [$answer = "yes"]
#do
# echo
# echo "2.Are you foolish? (yes/no) "
# Read Answer
#done
#
#answer = "No"
#
#until [$answer = "yes"]
#do
# echo
# echo "3.Do you Love Me? (yes/no) "
# Read Answer
#done
#
#e Cho "***********************************************************************"
#e Cho
#e Cho "Now! I know you is a foolish girl and you Love Me. "
#e Cho
#e Cho "So can continue"
#e Cho
#e Cho "**************************************************************"
#e Cho
#sleep 3
#e cho "(haha!) It is just a joke) "
I=1
echo "This time I was not equal to 4"
While [$i-le 8]
Do
((i=i+1))
If [$i-eq 4]
Then
echo "This time I am equal to 4"
Continue
Fi
echo "This time I was not equal to 4"
Done
echo "The command is $"
echo "The first argument is $, the second argument is $"
echo "The entire command is $ $ $"
echo "And there are $# arguments"
echo "The end of testing"
#! /bin/sh
If [$#-GT 1]
Then
echo "Too Many parameters"
Exit 1
Fi
If [$#-eq 0]
Then
echo "Too Few parameters"
Exit 100
Fi
if [!-D $]
Then
echo "Usage: $ directory"
Exit 1
Fi
#for I in $1/*
#do
# if [-X $i-A!-D $i]
# Then
# ls $i
# fi
#done
#!/bin/sh
rpm=/bin/rpm
option= "-Q"
While True
Do
echo "What to query?"
Select Var in ' All ' "file" "Package name"
Do
Case $var in
All)
option= $option "a"
Break
;;
File
Echo-n "Please input file name:"
Option= $option "F"
Read argument
Break
;;
package\ name)
Echo-n "Please input package name:"
Read argument
Break
;;
*)
echo "Please choose between 1-3"
;;
Esac
Done
echo "What does want to know?"
Select Var in ' location ' "info" "Package name"
Do
Case $var in
Location
option= $option "L"
Break
;;
Info
option= $option "I"
Break
;;
package\ name)
Break
;;
*)
echo "Please choose between 1-3"
;;
Esac
Done
${RPM} $option $argument
echo "Continue?" [Yes/no] "
Read answer
if [answer = "no"]
Then
Break
Fi
Done
#!/bin/sh
Sum=0
For a in ' SEQ 1 100 '
Do
If [' Expr $a% 3 '-ne 0]
Then
Continue
Fi
Echo $a
sum= ' expr $sum + $a '
Done
echo "sum = $sum"
#!/bin/bash
Ftype= ' file ' $ "'
Case ' $ftype ' in
"$1:zip Archive" *)
/usr/bin/unzip "$";;
"$1:gzip Compressed" *)
/bin/gunzip "$";;
"$1:bzip2 Compressed" *)
/usr/bin/bunzip2 "$";;
*)
echo "Sorry, file" can not is uncompressed with smartzip. ";
Esac
Note: There is a line above, for a in ' SEQ 1 10 ', where SEQ 1 10, that is, the column appears all the numbers between 1 and 10, this line can also be changed to: For A In "1 2 3 4 5 6 7 8 9 10", when using expr for multiplication, to avoid As a match, you need to escape it and use the \*
Automatic backup of database scripts two!
Mysql:linux a shell script that automatically backs up the database on a Linux server, the program updates the MySQL database every day, so it reminds me to write a shell script, combined with crontab, to back up the database regularly. In fact, it's very simple, mainly using MySQL's mysqldump command.
The script reads as follows:
#!/bin/sh
# File:/home/mysql/backup.sh
# Database Info
Db_name= "Test"
Db_user= "username"
db_pass= "Password"
# Others VARs
Bin_dir= "/usr/local/mysql/bin"
Bck_dir= "/home/mysql/backup"
Date= ' Date +%f '
# TODO
$BIN _dir/mysqldump--opt-u$db_user-p$db_pass $DB _name | gzip > $BCK _dir/db_$date.gz
Then use the Add this script to the/etc/crontab Timer task:
5 * * 0 mysql/home/mysql/backup.sh
Well, every Sunday 5:01 the system automatically runs the backup.sh file to back up the MySQL database.
/home/www/inc/back
A second!!!!
- #!/bin/sh
- # File:/home/mysql/backup.sh
- # Database Info Bakupmysql
- Db_user= "USER"
- db_pass= "Password"
- # Others VARs
- Date= ' Date +%y-%m-%d '
- mkdir/home/mysqlbak/$DATE
- Bin_dir= "/usr/local/mysql/bin"
- Bck_dir= "/home/mysqlbak/$DATE"
- # TODO
- $BIN _dir/mysqldump--opt-u$db_user-p$db_pass discuz > $BCK _dir/discuz.sql
- $BIN _dir/mysqldump--opt-u$db_user-p$db_pass zjblog > $BCK _dir/zjblog.sql
- $BIN _dir/mysqldump--opt-u$db_user-p$db_pass openads > $BCK _dir/openads.sql
Linux Shell Scripts