2016-05-13

dev
unknown 2016-05-13 21:42:54 +08:00
commit 847f5e7495
73 changed files with 861 additions and 0 deletions

View File

@ -0,0 +1,9 @@
#!/bin/bash
#退出状态码最大为255超过则进行模运算
#testing the exit status
var1=10
var2=20
var3=$[ $var1 + $var2]
echo The answer is $var3
exit 5

View File

@ -0,0 +1,8 @@
#!/bin/bash
#An example of using the expr command
var1=10
var2=20
var3=`expr $var2 / $var1`
echo "The result is $var3"

View File

@ -0,0 +1,15 @@
#!/bin/bash
var1=10.45
var2=43.67
var3=33.2
var4=71
var5=`bc <<EOF
scale=4
a1 = $var1 * $var2
b1 = $var3 * $var4
a1 + b1
EOF
`
echo The final answer for this mess is $var5

View File

@ -0,0 +1,7 @@
#!/bin/bash
var1=10
var2=50
var3=45
var4=$[$var1 * ($var2 - $var3)]
echo 'The final result is '$var4

View File

@ -0,0 +1,9 @@
#!/bin/bash
#testing variables
days=10
guest="Katie"
echo "$guest logged in $days days age"
guest="Katie2"
days=5
echo "$guest logged in $days days age"

View File

@ -0,0 +1,5 @@
#!/bin/bash
#using the backtick character
testing=`date`
echo "The date and time are:$testing"

View File

@ -0,0 +1,6 @@
#!/bin/bash
var1=100
var2=45
var3=`echo "scale=4; $var1 / $var2" | bc`
echo The answer for this is $var3

View File

@ -0,0 +1,12 @@
#!/bin/bash
#This script displays the date and who's logged on
#如果想在同一行显示
#echo -n -e 'The time is:\n\n'
echo The time is:
date
echo The one who has been logged is:
who

View File

@ -0,0 +1,10 @@
#!/bin/bash
#display user information from system
echo "User info fro userId:$USER"
echo UID:$UID
echo HOME:$HOME
#换行
echo -e '\n'
echo 'The cost of the item is \$15'

View File

@ -0,0 +1,5 @@
#!/bin/bash
#copy the /usr/bin directory listing to a log file
today=`date +%y%m%d`
ls /usr/bin -al > log.$today

View File

@ -0,0 +1,11 @@
#!/bin/bash
# reading data from a file
count=1
cat test | while read line
do
echo "Line $count: $line"
count=$[ $count + 1 ]
done
echo "Finished processing the file"

View File

@ -0,0 +1,12 @@
#!/bin/bash
# simple demonstration of the getopts command
while getopts :ab:c opt
do
case "$opt" in
a) echo "Found the -a option";;
b) echo "Found the -b option, with value $OPTARG";;
c) echo "Found the -c option";;
*) echo "Unknown option:$opt";;
esac
done

View File

@ -0,0 +1,20 @@
#!/bin/bash
# processing options and parameters with getopts
while getopts :ab:cd opt
do
case "$opt" in
a) echo "Found the -a option";;
b) echo "Found the -b option,with value $OPTARG";;
c) echo "Found the -c option";;
d) echo "Found the -d option";;
*) echo "Unknown option: $opt";;
esac
done
shift $[ $OPTIND - 1 ]
count=1
for param in "$@"
do
echo "Parameter $count: $param"
count=$[ $count + 1 ]
done

View File

@ -0,0 +1,25 @@
#!/bin/bash
#extracting command line options and values with getopt
# getopt command is not goot at dealing with space,we can use getopts
set -- `getopt -q ab:c "$@"`
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option";;
-b) param="$2"
echo "Found the -b option,with parameter value $param"
shift;;
-c) echo "Found the -c option";;
--) shift
break;;
*) echo "$1 is not an option";;
esac
shift
done
count=1
for param in "$@"
do
echo "Parameter #$count: $param"
count=$[ $count+1 ]
done

View File

@ -0,0 +1,17 @@
#!/bin/bash
# shift n 移动变量
count=1
while [ -n "$1" ]
do
echo "Parameter #$count = $1"
count=$[ $count+1 ]
shift
done
echo -e "\n"
# demonstrating a multi-position shift
echo "The original parameters : $*"
shift 2
echo "Here's the new first parameter: $1"

View File

@ -0,0 +1,23 @@
#!/bin/bash
#extracting options and parameters
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option";;
-b) echo "Found the -b option";;
-c) echo "Found the -c option";;
--) shift
break;;
*) echo "$1 is not an option";;
esac
shift
done
count=1
for param in $@
do
echo "Parameter #$count: $param"
count=$[ $count + 1 ]
done

View File

@ -0,0 +1,10 @@
#!/bin/bash
# getting the number of parameters
echo There were $# parameters supplied
#花括号里不能使用美元符号
params=$#
echo The last parameter is $params
echo The last parameter is ${!#}

View File

@ -0,0 +1,25 @@
#!/bin/bash
# extracting command line options and values
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option";;
-b) param="$2"
echo "Found the -b option, with parameter value $param"
shift;;
-c) echo "Found the -c option";;
--) shift
break;;
*) echo "$1 is not an option";;
esac
shift
done
count=1
for param in "$@"
do
echo "Parameter #$count : $param"
count=$[ $count + 1 ]
done

View File

@ -0,0 +1,13 @@
#!/bin/bash
# extracting command line options as parameters
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option";;
-b) echo "Found the -b optins";;
-c) echo "Found the -c optins";;
*) echo "$1 is not a valid options";;
esac
shift
done

View File

@ -0,0 +1,16 @@
#!/bin/bash
# testing $* and $@
count=1
for param in "$*"
do
echo "\$* Parameter #$count = $param"
count=$[ $count+1 ]
done
count=1
for param in "$@"
do
echo "\$@ Paramenter #$count = $param"
count=$[ $count+1 ]
done

View File

@ -0,0 +1,27 @@
#!/bin/bash
# testing the reading command
echo -n "Enter your name:"
read name
echo "Hello $name, welcome to my program"
read -p "Please enter your age: " age
days=$[ $age * 365 ]
echo "That makes you over $days days old"
#指定多个变量,输入的每个数据值都会分配给表中的下一个变量,如果用完了,就全分配各最后一个变量
read -p "Please enter name:" first last
echo "Checking data for $last. $first..."
#如果不指定变量read命令就会把它收到的任何数据都放到特殊环境变量REPLY中
read -p "Enter a number:"
factorial=1
for (( count=1; count<=$REPLY; count++))
do
factorial=$[ $factorial * $count ]
done
echo "The factorial of $REPLY is $factorial"

View File

@ -0,0 +1,10 @@
#!/bin/bash
# using one command line parameter
factorial=1
for (( number = 1; number <= $1; number++))
do
factorial=$[ $factorial * $number ]
done
echo The factor of $1 is $factorial

View File

@ -0,0 +1,8 @@
#!/bin/bash
# handing lots of parameters
total=$[ ${10} * ${11} ]
echo The tenth parameter is ${10}
echo The eleventh parameter is ${11}
echo The total is $total

View File

@ -0,0 +1,9 @@
#!/bin/bash
# testing the $0 parameter
echo The command entered is $0
#当传给$0变量的真实字符串是整个脚本的路径是程序中就会使用整个路径而不仅仅是程序名
name=`basename $0`
echo The command entered is $name

View File

@ -0,0 +1,22 @@
#!/bin/bash
# timing the data entry
if read -t 5 -p "Please enter your name:" name
then
echo "Hello, $name, welcome to my script"
else
#起到换行的作用
echo
#输入计数 -n1
read -n1 -p "Do you want to continue [Y/N]?" answer
case $answer in
Y | y) echo
echo "Fine, continue on...";;
N | n) echo
echo "OKgoodbye";;
*) echo
echo "OK, wrong, goodbye"
esac
echo "Sorry, this is the end of the script"
fi

View File

@ -0,0 +1,8 @@
#!/bin/bash
# hiding input data from monitor
read -s -p "Please enter your password: " pass
#添加了-s选项之后不会自动换行不添加-s 会自动换行
echo
echo "Is your password really $pass?"

View File

@ -0,0 +1 @@
#!/bin/bash

View File

@ -0,0 +1 @@
#!/bin/bash

View File

@ -0,0 +1 @@
#!/bin/bash

View File

@ -0,0 +1,10 @@
#!/bin/bash
#创建10个sh
for (( i=1; i<=4; i++))
do
touch $i.sh
echo '#!/bin/bash' > $i.sh
chmod 764 $i.sh
done

View File

@ -0,0 +1,2 @@
ls: cannot access badtest: No such file or directory
but this should go to the testerror file

View File

@ -0,0 +1 @@
This is the end of the script

View File

@ -0,0 +1,6 @@
#!/bin/bash
# testing STDERR messages
echo "This is an error " >&2
echo "This is another error"
echo "This is also an error" >&2

View File

@ -0,0 +1,12 @@
#!/bin/bash
# redirecting the inpiut
# 从test中读取数据而不是从STDIN中读取数据
exec 0< test
count=1
while read line
do
echo "Line #$count : $line "
count=$[ $count +1 ]
done

View File

@ -0,0 +1,14 @@
#!/bin/bash
# testing STDERR messages
# redirecting all to a file
# 脚本执行期间用exec命令告诉shell重定向某个特定文件描述符
exec 2>test
ls badtest
echo "This is test of redirecting all output"
echo "from a script to another file"
exec 1>test1
echo "This is the end of the script"
echo "but this should go to the testerror file" >&2

View File

@ -0,0 +1,11 @@
#!/bin/bash
#bash shell 仅能处理浮点数值,test命令无法处理val1变量中存储的浮点值
#testing floating point numbers
val1=`echo "scale=4; 10 / 3" | bc`
echo "The test value is $val1"
if [ $val1 -gt 3 ]
then
echo "The result is larger than 3"
fi

View File

@ -0,0 +1,16 @@
#!/bin/bash
#continuing an outer loop
for (( a=1; a<=5; a++))
do
echo "Iteration $a:"
for (( b=1; b<3; b++ ))
do
if [ $a -gt 2 ] && [ $a -lt 4 ]
then
continue 2
fi
var3=$[ $a * $b ]
echo " The result of $a * $b is $var3"
done
done

View File

@ -0,0 +1,10 @@
#!/bin/bash
#testing the else section
testuser=badtest
if grep $testuser /etc/passwd
then
echo The files for user $testuser are:
ls -a /home/.b*
else
echo "The user name $testuser does not exist on this system"
fi

View File

@ -0,0 +1,12 @@
#!/bin/bash
#testing the if statement
if date
then
echo "it worked"
fi
echo -e '\n'
if asd
then
echo "it not worked"
fi
echo 'We are outside the if statement'

View File

@ -0,0 +1,12 @@
/home/tiandi/Desktop is a directory
/home/tiandi/Documents is a directory
/home/tiandi/Downloads is a directory
/home/tiandi/examples.desktop is a file
/home/tiandi/lamp is a directory
/home/tiandi/Music is a directory
/home/tiandi/Pictures is a directory
/home/tiandi/Public is a directory
/home/tiandi/sh is a directory
/home/tiandi/Templates is a directory
/home/tiandi/test is a directory
/home/tiandi/Videos is a directory

View File

@ -0,0 +1,16 @@
#!/bin/bash
# placing a for loop inside a while loop
var1=5
while [ $var1 -ge 0 ]
do
echo "Outer loop: $var1"
for (( var2=1; $var2 < 3; var2++))
do
var3=$[ $var1*$var2]
echo "Inner loop: $var1 * $var2 = $var3"
done
var1=$[ $var1 - 1 ]
done

View File

@ -0,0 +1,11 @@
#!/bin/bash
#nesting for loops
for (( a=1; a<=3; a++ ))
do
echo "Starting loop $a:"
for (( b=1; b<=3; b++))
do
echo "Inside loog: $b:"
done
done

View File

@ -0,0 +1,11 @@
#!/bin/bash
# using a variable to hold the list
list="Alabama Alaska Arizona"
list=$list" Connecticut"
for state in $list
do
echo "Have you ever visited $state"
done

View File

@ -0,0 +1,18 @@
#!/bin/bash
#reading values from a file
file="state"
#更改字段分隔符,使其只能识别换行符
IFS=$'\n'
#处理长脚本是,在一个地方修改了该值,然后可能忘了修改过该值
#IFS.OLD=$IFS
#IFS=$'\n'
#具体代码
#IFS=$IFS.OLD
for state in `cat $file`
do
echo "Visit beautiful $state"
done

View File

@ -0,0 +1,7 @@
#!/bin/bash
#testing the C-style for loop
for (( i=1; i<=10; i++ ))
do
echo "The next number is $i"
done

View File

@ -0,0 +1,16 @@
#!/bin/bash
# break n默认为1
for (( a=1; a<=3; a++ ))
do
echo "Outer loop : $a"
for (( b=1; b < 100; b++ ))
do
if [ $b -gt 4 ]
then
break 2
fi
echo " Inner loop:$b"
done
done

View File

@ -0,0 +1,15 @@
#!/bin/bash
#using the case command
case $USER in
tiandi | barbar)
echo "Welcome, $USER"
echo "Pleas enjoy your visit";;
testing)
echo "Special testing account";;
jessica)
echo "Do not forget to logout when you are out";;
*)
echo "Sorry, you are not allowed here";;
esac

View File

@ -0,0 +1,12 @@
#!/bin/bash
#using the continue command
for (( var1 = 1; var1 < 15; var1++ ))
do
if [ $var1 -gt 5 ] && [ $var1 -lt 10 ]
then
continue
fi
echo "Iteration number:$var1"
done

View File

@ -0,0 +1,19 @@
#!/bin/bash
# looking for a possible value
if [ $USER = "tiandi" ]
then
echo "Welcome $USER"
echo "Please enjoy your visit"
elif [ $USER = testing ]
then
echo "Welcome $USER"
echo "Please enjoy your visit"
elif [ $USER = barbar ]
then
echo "Do not forget to logout when you're done"
else
echo "Sorry, you are not allowed here"
fi

View File

@ -0,0 +1,15 @@
#!/bin/bash
var1=10
var2=5
if [ $var1 -gt 5 ]
then
echo "The test value $var1 is greater than 5"
fi
if [ $var1 -eq $var2 ]
then
echo "The values is equal"
else
echo "The values are different"
fi

View File

@ -0,0 +1,21 @@
#!/bin/bash
#using the until command
var1=100
until [ $var1 -eq 0 ]
do
echo $var1
var1=$[ $var1-25 ]
done
var1=100
until echo $var1
[ $var1 -eq 0 ]
do
echo Inside the loop: $var1
var1=$[ $var1 - 25 ]
done

View File

@ -0,0 +1,10 @@
#!/bin/bash
# while command test
var1=10
while [ $var1 -gt 0 ]
do
echo $var1
var1=$[ $var1 - 1 ]
done

View File

@ -0,0 +1,11 @@
#!/bin/bash
#testing a multicommand while loop
var1=10
while echo $var1
[ $var1 -ge 0 ]
do
echo 'This is inside the loop'
var1=$[ $var1 - 1 ]
done

View File

@ -0,0 +1,11 @@
#!/bin/bash
# using double parenthesis
var1=10
if (( $var1 ** 2 > 90))
then
(( var2 = $var1 ** 2))
echo "The square of $var1 if $var2"
fi

View File

@ -0,0 +1,7 @@
#!/bin/bash
# another example of how not to use the for command
for test in Newada "New Hampshire"
do
echo "Now going to $test"
done

View File

@ -0,0 +1,9 @@
#!/bin/bash
# using pattern matching
if [[ $USER == r* ]]
then
echo "Hello $USER"
else
echo "Sorry, I do not know you"
fi

View File

@ -0,0 +1,15 @@
#!/bin/bash
#changing the IFS value
IFS.OLD=$IFS
IFS=$'\n'
for entry in `cat /etc/passwd`
do
echo "Values in $entry -"
IFS=:
for value in $entry
do
echo " $value"
done
done

View File

@ -0,0 +1,9 @@
#!/bin/bash
#testing compound comparisons
if [ -d $HOME ] && [ -w $HOME/testing ]
then
echo "The file exists and you can write to it"
else
echo "I cannot write to it"
fi

View File

@ -0,0 +1,20 @@
#!/bin/bash
# redirecting the for output to a file
for file in /home/tiandi/*
do
if [ -d "$file" ]
then
echo "$file is a directory"
else
echo "$file is a file"
fi
done > output.txt
# piping a loop to another command
for state in "North Dakota" Connecticut
do
echo "$state is next place to go"
done | sort
echo "This completes our travels"

View File

@ -0,0 +1,12 @@
#!/bin/bash
#iterate through all the files in a directory
for file in /home/tiandi/test/*
do
if [ -d "$file" ]
then
echo "$file is a directory"
elif [ -f "$file" ]
then
echo "$file is a file"
fi
done

View File

@ -0,0 +1,29 @@
#!/bin/bash
# testing string length
#-n 判断长度是否非零
#-z 判断长度是否为零
val1=testing
val2=''
if [ -n "$val1" ]
then
echo "The string $val1 is not empty"
else
echo "The string $val1 is empty"
fi
if [ -z "$val2" ]
then
echo "The string $val2 is empty"
else
echo "The string $val2 is not empty"
fi
if [ -z "$val3" ]
then
echo "The string $val3 is empty"
else
echo "The string $val3 is not empty"
fi

View File

@ -0,0 +1,7 @@
#!/bin/bash
# multiple variables
for (( a=1, b=10; a<=10; a++,b-- ))
do
echo "$a - $b"
done

View File

@ -0,0 +1,8 @@
#!/bin/bash
#testing multiple commands in the then section
testuser=tiandi
if grep $testuser /etc/passwd
then
echo The bash files from user $testuser are:
ls -a /home/$testuser/.b*
fi

View File

@ -0,0 +1,9 @@
#!/bin/bash
#testing string equality
testuser=tiandi
if [ $USER = $testuser ]
then
echo "Welcome $testuser"
fi

View File

@ -0,0 +1,2 @@
#!/bin/bash

View File

@ -0,0 +1,20 @@
#!/bin/bash
# checking if a directory or a file exists
if [ -e $HOME ]
then
echo "OK on the directory.now to check the file"
#checking if a file exists
if [ -e $HOME/testing ]
then
#the file exists,append data to it
echo "Appending date to existing file"
date >> $HOME/testing
else
#the file is not exists,create a new file
echo "Creating a new file"
date > $HOME/testing
fi
else
echo 'Sorry. you do not have a $HOME directory'
fi

View File

@ -0,0 +1,11 @@
#!/bin/bash
# look before you leap
if [ -d $HOME ]
then
echo "Your home directory exists"
cd $HOME
ls -a
else
echo "There is a problem with your HOME direcotry"
fi

View File

@ -0,0 +1,12 @@
#!/bin/bash
#正确使用大于小于号
val1=baseball
val2=hocky
if [ $val1 \> $val2 ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi

View File

@ -0,0 +1,12 @@
#!/bin/bash
#test命令中大小字母会被当成小于小写字符而在sort中小写字母会先出现,test使用标准的ASCII排序sort使用本地化语言设置进行排序对于英语本地化设置制定了排序顺序中小写字母出现在大写字母之前
var1=Testing
var2=testing
if [ $val1 \> $val2 ]
then
echo '$val1 is greater than $val2'
else
echo '$val1 is less than $val2'
fi

View File

@ -0,0 +1,6 @@
#!/bin/bash
for test in I don\'t know if "this'll" work
do
echo "word:$test"
done

View File

@ -0,0 +1,7 @@
#!/bin/bash
# basic for command
for test in Alabama Alaska Arizona
do
echo The next state is $test
done

View File

@ -0,0 +1,8 @@
#!/bin/bash
# another example of how not to use the for command
for test in I don't know if this'll work
do
echo "word:$test"
done

View File

@ -0,0 +1,14 @@
#!/bin/bash
# 大于小于号必须转义否则shell会将它们当做重定向符号而把字符串值当做文件名处理
# 大于小于号顺序和sort命令所采用的有所不同
# mis-using string comparisons
val1=baseball
val2=hockey
if [ $val1 > $val2 ]
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi