linuxshell 读书笔记 (unix shell 第三版)

来源:互联网 发布:excel数据录入程序 编辑:程序博客网 时间:2024/05/15 23:43
把一些简单的命令放入文件中 如 cat echo_script
#echo demo
echo the current data and time is:
date
echo
echo the number of users on the system is:
who|wc -l
echo
echo your current working directory is :
pwd

修改echo_script文件属性 使其可执行 chmod 755 echo_script
执行这个脚本                         ./echo_script
在当前目录上创建两个文件夹           mkdir -p xep/work

[xep@localhost ~]$ cat variable_script
#variable demo
my_bin="/home/xep/xep/work"
number=99
cd $my_bin
 pwd
echo there are $number beer on the wall

command=sort
cd ../..
$command test1 2>null
修改脚本属性  执行后看结果
cat cmd_script
#put command to variable
cmd=sort
para=-r
file=test.123
运行时  shell会在执行命令前把执行的命令和参数进行替换

对于空变量的解释 echo :$xxxxx:

x=*
echo $x, '$x', "$x"--当前目录下文件名,'$x',*

mv $file ${file}_1 --将文件名重命名为之前文件名_1

算术扩展 $(( ))   --不支持带小数的计算
echo $((i+10))
echo result=(($i>0 && $j>0)) --真返回1否则为0

输入命令查看结果 --不支持带小数的计算
expr 2 + 3   expr 2+3  expr 2 * 3  expr 2 \* 3  

 bc 命令 带小数计算

[xep@localhost ~]$ lines=one'
> two
> 'three
[xep@localhost ~]$ echo $lines --最后输入的'表示换行
one two three

[xep@localhost ~]$ lines=one\
> \two
[xep@localhost ~]$ echo $lines --与'的作用相似
onetwo

[xep@localhost ~]$ echo your current working directory is `pwd`
your current working directory is /home/xep
--shell将所有``中命令的标准输入放在当前位置 所有用$()的地方可换为``

[xep@localhost ~]$ cat args_script
echo $# arguments passed
echo arg 1=:$1: arg 2=:$2: arg 3=:$3:

输入命令查看结果
./args_script $(cat file)    --将file中的内容做为参数  $# 输入参数的个数
./args_script t*      --将当前目录中t开头的文件为参数

[xep@localhost ~]$ cat args2_script
echo there are $# parameter passed
echo there are $*

输入命令查看结果
./args2_script $(cat file)    --将file中的内容做为参数  $* 所有输入参数的内容
./args2_script t*      --将当前目录中t开头的文件为参数    

[xep@localhost ~]$ cat args3_script
declare directory=/home/xep
file=phonebook
grep "$1" ${directory}/$file

输入命令查看结果

./args3_script "Alica chebba"  --如果不在"$1"外加双引号 再试试



原创粉丝点击