shell基础(转)
学习Unix有很长一段时间了,下一段时间可能很少有机会搞Unix了!
我毕业设计要用MFC,进单位以后也是Windows下的编程!
总结一下吧,也算是对Unix的学习告一段落!
书籍:《Unix Text Book》
一、运行Bourne shell脚本
1、$chmod u+x script_file
2、/bin/sh script_file
二、shell变量及相关命令
shell变量的两种类型,shell环境变量和用户自定义变量
shell环境变量用于定制shell的运行环境并保证shell命令的正确运行。大部分shell环境变量都在
登陆过程中执行/etc/.profile启动文件时进行初始化。该文件由系统管理员设置,它为系统上所
有用户建立一个公共的环境。通过更改你的~/.profile启动文件就能定制自己的环境变量。
一些只读的Borune Shell环境变量
$0 程序名称
$1 - $9 前九个命令行参数的值
$* 所有命令行参数的值
$@ 所有命令行参数的值,如果它被包含在引号里,形如"$@",则每个参数也各自被引号包括
$# 命令行参数的个数
$$ 当前进程的进程ID(PID)
$? 最近使用命令的退出状态
$! 最近后台进程的进程ID
用户自定义变量在shell脚本里使用,它们拥有临时的存储空间,在程序执行过程中变量
的值可能改变。这些变量可被设置为只读,也可以被传递给定义它们的shell脚本里的命令。
Bourne Shell脚本中无需声明和初始化shell变量。一个未初始化的shell变量的默认值为空字符串
。
三、一些简单的例子(例子比较简单,大家运行一下,好好体会体会)
1、
#!my first bash file
#one sample example
pwd
2、
#!/bin/sh
echo "Enter input :\c"
read line
echo "You entered : $line"
echo "Enter another line : \c"
read word1 word2 word3
echo "The first word is : $word1"
echo "The second word is: $word2"
echo "The third word is : $word3"
exit 0
3、
#!/bin/sh
echo "The command name is : $0."
echo "The number of command line argments passed as parameter are $#."
echo "The value of the command line argument are: $1 $2 $3 $4 $5 $6 $7 $8 $9."
echo "another way to display value of all of the argument : $@."
echo "Yet another way is : $*."
exit 0
4、#shift.sh
#!/bin/sh
echo "The program name is $0."
echo "The arguments are : $@."
echo "The first three arguments are : $1 $2 $3"
shift
echo "The program name is $0."
echo "The argument are : $*."
echo "The first three arguments are : $1 $2 $3"
shift 3
echo "The program name is $0."
echo "The argument are : $*."
echo "The first three arguments are : $1 $2 $3"
5、#set1.sh
#!/bin/sh
filename="$1"
set `ls -il $filename`
inode="$1"
size="$6"
echo "Name Inode Size"
echo "$filename $inode $size"
exit 0
6、#if1.sh
#!/bin/sh
if test $# -eq 0
then
echo "Usage : $0 ordinary_file"
exit 1
fi
if test $# -gt 1
then
echo "Too many arguments"
echo "Usage : $0 ordinary_file"
exit 1
fi
if test -f $1
then
#!/bin/sh next is get by "cat" from set1
filename="$1"
set `ls -il $filename`
inode="$1"
size="$6"
echo "Name Inode Size"
echo "$filename $inode $size"
exit 0
fi
#other condition
echo "$0: argument must be an ordinary_file"
exit 1
7、#for1.sh
#!/bin/sh
for people in Debbie Jamie John Kitty Kuhn Shah
do
echo "$people" #use or not use "" has the same result
done
exit 0
8、#for2.sh
#!/bin/sh
#create use "e+filename"
#!Create by using "e+filename"
for user
do
echo $user
done
exit 0
9、#while.sh
#!/bin/sh
secretcode=agent007
echo "Guess the code"
echo "Enter you guess:"
read yourguess
while [ $yourguess != $secretcode ]
do
echo "Good guess but wrong.Try again !"
echo "Enter you guess : "
read yourguess
done
echo "Wow ! You are a genius !"
exit 0
10、#until.sh
#!/bin/sh
secretcode=agent007
echo "Guess the code"
echo "Enter you guess:"
read yourguess
until [ $yourguess = $secretcode ]
do
echo "Good guess but wrong.Try again !"
echo "Enter you guess : "
read yourguess
done
echo "Wow ! You are a genius !"
exit 0
11、#case.sh
#!/bin/sh
#using case
echo "Use ome of the following options:"
echo "d or D: to display today's date and present time"
echo "l or L: to see the listing of files in your present working directory"
echo "w or W: to see who's logged in"
echo "q or Q: to quit this program"
echo "Enter you option and hit <enter> :"
read option
case $option in
d|D) date;;
l|L) ls;;
w|W) whoami;;
q|Q) exit 0;;
*) echo "Invalid option! Try running the program again."
exit 1
;;
esac
exit 0