码迷,mamicode.com
首页 > 系统相关 > 详细

Linux Shell

时间:2015-09-22 19:00:05      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

一、运行原理

    Shell 是用户和Linux系统之间进行交互的接口程序,是一组机器可以理解的指令集,并通过用户的指令集来实现用户所需要的功能,原理如下图:

    技术分享

   一般格式:为以".sh"为结尾的文件,并以#!/bin/bash头起顶格写,以#开头为注释文件,有时,根据具体情况,以及权限、运行环境的不同会在系统启动时(/etc/fstab),系统特定时间运行特定的bash(crontab -e),并且在一个shell中一般注明脚本的作者,版本,用途等备注信息

linux 进程、执行方式、程序、编程元素

http://canshan.blog.51cto.com/2613522/1695620

Linux 脚本 命令状态 条件测试

http://canshan.blog.51cto.com/2613522/1695858

Linux shell的条件判断、循环语句及实例

http://canshan.blog.51cto.com/2613522/1696292

linux 函数

http://canshan.blog.51cto.com/2613522/1696378

linux sed awk

http://canshan.blog.51cto.com/2613522/1696411


1、如果某路径不存在,则将其创建为目录;否则显示其存在,并显示内容类型

技术分享

2、断给定的两个数值,孰大孰小;给定数值的方法:脚本参数,命令交互;(使用read)

[root@iZ28qzns9m4Z mageshell]# sh -n 1number.sh 
[root@iZ28qzns9m4Z mageshell]# sh -x 1number.sh 
+ read -t 10 -p ‘please input two number: ‘ num1 num2
please input two number: 1
+ ‘[‘ -z 1 ‘]‘
+ ‘[‘ -z ‘‘ ‘]‘
+ echo ‘please input other one‘
please input other one
+ exit 1
[root@iZ28qzns9m4Z mageshell]# sh -x 1number.sh 
+ read -t 10 -p ‘please input two number: ‘ num1 num2
please input two number: 1 2
+ ‘[‘ -z 1 ‘]‘
+ ‘[‘ -z 2 ‘]‘
+ ‘[‘ 1 -ge 2 ‘]‘
+ echo ‘Max: 2,Min: 1‘
Max: 2,Min: 1
[root@iZ28qzns9m4Z mageshell]# cat 1number.sh 
#!/bin/bash
read -t 10 -p "please input two number: " num1 num2
if [ -z "$num1" ] ||[ -z "$num2" ]; then
	echo "please input other one"
	exit 1
fi
if [ $num1 -ge $num2 ]; then
	echo "Max: $num1,Min: $num2"
else
	echo "Max: $num2,Min: $num1"
fi
[root@iZ28qzns9m4Z mageshell]#

3、求100以内所有奇数之和

①[root@iZ28qzns9m4Z mageshell]# sh 1sum.sh 
2500
[root@iZ28qzns9m4Z mageshell]# cat 1sum.sh 
#!/bin/bash
declare -i sum=0
for i in {1..100..2}; do
		sum=$[ $sum+$i ]
done
echo $sum
[root@iZ28qzns9m4Z mageshell]# sh -x 1sum.sh
②[root@iZ28qzns9m4Z mageshell]# sh 2sum.sh 
sum:2500
[root@iZ28qzns9m4Z mageshell]# cat 2sum.sh 
#!/bin/bash
sum=0
for i in $(seq 1 2 100)
do
	let "sum=sum+i"
done
echo "sum:$sum"
[root@iZ28qzns9m4Z mageshell]#
③+ ‘[‘ 101 -lt 100 ‘]‘
+ echo 2500
2500
[root@iZ28qzns9m4Z mageshell]# cat 3sum.sh 
#!/bin/bash
declare -i sum=0
declare -i i=1
while [ $i -lt 100 ]; do
	let sum+=$i
	let i+=2
done
echo $sum
[root@iZ28qzns9m4Z mageshell]#

注:seq 是Linux 中一个预设的外部命令,一般用作一堆数字的简化写法。

参数:

-f, --format=FORMAT  use printf style floating-point FORMAT (default: %g)

-s, --separator=STRING use STRING to separate numbers (default: \n)

-w, --equal-width  equalize width by padding with leading zeroes

-f 最常用 , 例如一次制做 10 个名 dir001 , dir002 .. dir010 的目录 mkdir $(seq -f ‘dir%03g‘ 1 10)

[root@iZ28qzns9m4Z mageshell]# mkdir $(seq -f ‘dir%03g‘ 1 10)
[root@iZ28qzns9m4Z mageshell]# ls
1file2.sh   1sum.sh  dir001  dir003  dir005  dir007  dir009
1number.sh  2sum.sh  dir002  dir004  dir006  dir008  dir010

-s 选项主要改变输出的分格符, 预设是 \n , 就是 newline

[root@iZ28qzns9m4Z mageshell]# seq  1 2 100
1
3
5
7
9
11
13
15

4、写一个脚本实现如下功能:

(1) 传递两个文本文件路径给脚本;

(2) 显示两个文件中空白行数较多的文件及其空白行的个数;

(3) 显示两个文件中总行数较多的文件及其总行数;

[root@iZ28qzns9m4Z mageshell]# sh 2file.sh 
plz input two file:aa.txt bb.txt
max is: bb.txt
space is 4
bbbbbbbbbbbbbbbbbbbbbbb

bbbbbbbbbbbbbbbbbbbbbbb

bbbbbbbbbbbbbbbbbbbbbbb

bbbbbbbbbbbbbbbbbbbbbbb

bbbbbbbbbbbbbbbbbbbbbb
[root@iZ28qzns9m4Z mageshell]# cat 2file.sh 
#!/bin/bash
read -t 40 -p "plz input two file:" file1 file2
grep1=` grep "^$" $file1 | wc -l` 
grep2=` grep "^$" $file2 | wc -l` 
if [ $grep1 -gt $grep2 ]; then
	echo "max is:" $file1
	echo "space is $grep1"
else
	echo "max is:" $file2
	echo "space is $grep2"
fi 
if [ `cat $file1 | wc -l` -gt `cat $file2 | wc -l` ]; then
	echo "` cat $file1 `"
else
	echo "` cat $file2 `"
fi 
[root@iZ28qzns9m4Z mageshell]#


5、写一个脚本

(1) 提示用户输入一个字符串;

(2) 判断:如果输入的是quit,则退出脚本;否则,则显示其输入的字符串内容;

[root@iZ28qzns9m4Z mageshell]# sh -x bx1.sh 
+ read -t 50 -p ‘input a nstring:‘ string
input a nstring:aaaaaaa
+ ‘[‘ aaaaaaa == quit ‘]‘
+ echo aaaaaaa
aaaaaaa
[root@iZ28qzns9m4Z mageshell]# sh bx1.sh 
input a nstring:aaaaaaaaaaaaaa
aaaaaaaaaaaaaa
[root@iZ28qzns9m4Z mageshell]# sh bx1.sh 
input a nstring:quit
thanks
[root@iZ28qzns9m4Z mageshell]#

打印2^n表;n等于一个用户输入的值

不是太理解,看的同学的

[root@iZ28qzns9m4Z mageshell]# sh 1number1.sh 
input a number:4
1
2
2x2=4
2x2x2=8
2x2x2x2=16
[root@iZ28qzns9m4Z mageshell]# sh -x 1number1.sh 
+ read -t 50 -p ‘input a number:‘ number
input a number:3
+ count=2
++ seq 0 3
+ for i in ‘$(seq 0 $number)‘
+ ‘[‘ 0 -eq 0 ‘]‘
+ echo -e 1
1
+ for i in ‘$(seq 0 $number)‘
+ ‘[‘ 1 -eq 0 ‘]‘
+ ‘[‘ 1 -eq 1 ‘]‘
+ echo -e 2
2
+ for i in ‘$(seq 0 $number)‘
+ ‘[‘ 2 -eq 0 ‘]‘
+ ‘[‘ 2 -eq 1 ‘]‘
+ ‘[‘ 2 -gt 1 ‘]‘
+ count+=x2
+ echo 2x2=4
2x2=4
+ for i in ‘$(seq 0 $number)‘
+ ‘[‘ 3 -eq 0 ‘]‘
+ ‘[‘ 3 -eq 1 ‘]‘
+ ‘[‘ 3 -gt 1 ‘]‘
+ count+=x2
+ echo 2x2x2=8
2x2x2=8
[root@iZ28qzns9m4Z mageshell]#

写一个脚本,写这么几个函数:函数1、实现给定的两个数值的之和;函数2、取给定两个数值的最大公约数;函数3、取给定两个数值的最小公倍数;关于函数的选定、两个数值的大小都将通过交互式输入来提供。

想了很久,没有思路,,,但会继续更新。。请关注


Linux Shell

标签:

原文地址:http://canshan.blog.51cto.com/2613522/1697180

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!