shell练习01 九九乘法表
九九乘法表如下所示:
1×1=1 | ||||||||
1×2=2 | 2×2=4 | |||||||
1×3=3 | 2×3=6 | 3×3=9 | ||||||
1×4=4 | 2×4=8 | 3×4=12 | 4×4=16 | |||||
1×5=5 | 2×5=10 | 3×5=15 | 4×5=20 | 5×5=25 | ||||
1×6=6 | 2×6=12 | 3×6=18 | 4×6=24 | 5×6=30 | 6×6=36 | |||
1×7=7 | 2×7=14 | 3×7=21 | 4×7=28 | 5×7=35 | 6×7=42 | 7×7=49 | ||
1×8=8 | 2×8=16 | 3×8=24 | 4×8=32 | 5×8=40 | 6×8=48 | 7×8=56 | 8×8=64 | |
1×9=9 | 2×9=18 | 3×9=27 | 4×9=36 | 5×9=45 | 6×9=54 | 7×9=63 | 8×9=72 | 9×9=81 |
1. Java实现:
1 public class _99Table { 2 public static void main(String args[]) 3 { 4 for(int i=1;i<=9;i++) 5 { 6 for(int j=1;j<=i;j++) 7 { 8 System.out.print(j+"*"+i+"="+i*j+'\t');// \t tab,缩进 9 } 10 System.out.println(); 11 } 12 13 } 14 15 }
运行结果:
1 1*1=1 2 1*2=2 2*2=4 3 1*3=3 2*3=6 3*3=9 4 1*4=4 2*4=8 3*4=12 4*4=16 5 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 6 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 7 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 8 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 9 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
2. Shell实现-------01 (for+while)
1 #!/bin/bash 2 for i in 1 2 3 4 5 6 7 8 9 3 do 4 j=1 5 while [ $j -le $i ] 6 do 7 # echo -n "$i*$j=$[$i*$j] " #can't aligned 8 # printf "$i*$j=$[$i*$j]\t" 9 echo -ne "$i*$j=$[$i*$j]\t" 10 j=$[$j+1] 11 done 12 echo -e '\n' 13 done
运行结果:
1 1*1=1 2 3 2*1=2 2*2=4 4 5 3*1=3 3*2=6 3*3=9 6 7 4*1=4 4*2=8 4*3=12 4*4=16 8 9 5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 10 11 6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 12 13 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 14 15 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 16 17 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
说明:我看了很多网上相关程序,都采用了第一行注释代码中公式后面空出几个字符来控制每一行公式与公式之间的空白。采用这个代码运行以后你会发现公式是对不齐的。因为1 和 14它们所占的字符的大小不一样啊。第二行注释采用了printf,这个暂且不讨论,后面会专门写篇文章来练习它的用法。因此,本篇着重练习echo.
3. echo用法
echo主要是用来显示一行文本信息,它有很多选项供你参考使用,在命令窗口中输入man echo后,就会得到如下信息(pls坚持阅读英文):
DESCRIPTION
Echo the STRING(s) to standard output. #echo输入的是字符串
-n do not output the trailing newline #取消行末的换行符号
-e enable interpretation of backslash escapes #启用反斜杠控制字符的转换
-E disable interpretation of backslash escapes (default) #禁用反斜杠控制字符的转换
If -e is in effect, the following sequences are recognized: #如果启动了反斜杠控制字符转换,关于echo
命令所支持的反斜杠控制字符如下:
\\ backslash #反斜杠本身
\a alert (BEL) #从系统的喇叭送出铃声(为嘛我练习的时候没有听见声音???)
\b backspace #向左退格键
\c produce no further output #阻止进一步的输出
\e escape #调整终端字体颜色样式等,等同ESC键
\f form feed #换页字符
\n new line #换行字符
\r carriage return #回车键
\t horizontal tab #水平缩进
\v vertical tab #垂直缩进
\0NNN byte with octal value NNN (1 to 3 digits) #ASCII 八进制编码(以x开头的为十六进制),此处的NNN为数字
\xHH byte with hexadecimal value HH (1 to 2 digits)
举例说明用法:
(1)-n
[Hermioner@localhost Documents]$ echo -n "abc";echo "def";echo "ghi"
abcdef
ghi
[Hermioner@localhost Documents]$
解释:输入了三个echo语句,用分号隔开。在第一个echo中使用了-n选项,就会取消文本abc末尾的换行符,并接着输出def ;而第二句默认文本末尾是有换行符的,因此,第三句的ghi换行输出。
note: echo语句中加引号和不加引号的区别。比如上面的例子,加不加对输出都没有影响。但是对于那种字符间本身有引号的就需要添加引号来保证原样输出:比如:
[Hermioner@localhost Documents]$ echo "Don't miss doing any good thing no matter how insignificant it looks"
Don't miss doing any good thing no matter how insignificant it looks
#能够正确输出我们想要表达的
[Hermioner@localhost Documents]$ echo Don't miss doing any good thing no matter how insignificant it looks
> '
Dont miss doing any good thing no matter how insignificant it looks
[Hermioner@localhost Documents]$
#这个不能正常输出,如果不加引号,回车以后,就会有个>符号,让我们继续输入字符,没办法,只好输入一个引号来匹配Don't中的引号,结果最终输出竟然一个引号都没有了。因此,总结一点,如果内含单引号,需要额外添加双引号;如果内含双引号,需要额外添加单引号。并且,关于变量处理部分也需要注意双引号和单引号的区别很大:单引号:纯字符串 ,双引号:可以解析变量。单引号比双引号更快,因为双引号会判断有没有变量。比如:
[Hermioner@localhost Documents]$ a=1
[Hermioner@localhost Documents]$ echo "$a+1"
1+1 #解析了变量 $a=1
[Hermioner@localhost Documents]$ echo '$a+1'
$a+1 #纯字符串
(2)-e 配合转义字符使用
1)\\
[Hermioner@localhost Documents]$ echo -e \\
\
[Hermioner@localhost Documents]$
2)\a 没听见声音,有谁知道咋弄不???
3)\b
[Hermioner@localhost Documents]$ echo -e abc\bdef
abcbdef
[Hermioner@localhost Documents]$ echo -e "abc\bdef"
abdef
[Hermioner@localhost Documents]$ echo -e 'abc\bdef'
abdef
对比上面三个echo语句的输出,做以下分析:第一条echo语句\b没有生效啊,我认为是shell预处理的原因,先执行echo abc\bdef,输出了abcbdef,然后才执 行了-e操作来处理特殊字符\b,但是此时预处理的结果中没有\b了,因此结果就是abcbdef。(欢迎纠错指正哈??)
4)\c (note:很多文章说是和" -n "一样的作用,但是根据英文翻译以及实践结果来看,不!一!样!!!
[Hermioner@localhost Documents]$ echo -e "ab\cd"
ab[Hermioner@localhost Documents]$
#输出结果\c后面的东西没有了
5)\e
调整终端字体颜色样式等,将会写篇文章专门来讨论它的用法
6)\t
参考九九乘法表用法
暂时写这么多例子,需要的时候再查找。主要是要知道,可以实现哪些功能,命令参数记不住没关系
4. for用法
for命令的基本格式是:
for var in list
do
commands
done
(1)可以读取列表中的值
[Hermioner@localhost Documents]$ bash test1
a
b
c
[Hermioner@localhost Documents]$ cat test1
#!bin/bash
for i in a b c
do
echo $i
done
[Hermioner@localhost Documents]$
(2)可以读取列表中的复杂值
[Hermioner@localhost Documents]$ cat test2
#!/bin/bash
for i in I "don't" know if this\'ll work
do
echo $i
done
[Hermioner@localhost Documents]$ bash test2
I
don't
know
if
this'll
work
[Hermioner@localhost Documents]$
上面shell脚本中使用了两种办法来处理字符串中的引号按原样输出:第一个单引号地方添加双引号;第二个单引号地方添加转义字符。
(3)从变量读取列表
直接将列表保存再变量中,然后引用变量就可以了。比如:
list=" a b c d"
list=$list
for i in $list
(4)从命令读取
比如:
for i in $(cat $test.sh)
(5)用通配符读取目录
for file in /home/test/* ,for命令会遍历/home/test/*的输出结果 比如:
[Hermioner@localhost Documents]$ ls -l
total 12
drwxrwxr-x. 2 Hermioner Hermioner 6 Jul 26 02:28 a
-rwxrwxr-x. 1 Hermioner Hermioner 45 Jul 26 01:59 test1
-rw-rw-r--. 1 Hermioner Hermioner 73 Jul 26 02:13 test2
-rwxrwxr-x. 1 Hermioner Hermioner 188 Jul 26 02:34 testfile
[Hermioner@localhost Documents]$ cat testfile
#!/bin/bash
for i in /home/Hermioner/Documents/*
do
if [ -d "$i" ]
then
echo "$i is a directory"
elif [ -f "$i" ]
then
echo "$i is a file"
fi
done
[Hermioner@localhost Documents]$ ./testfile
/home/Hermioner/Documents/a is a directory
/home/Hermioner/Documents/test1 is a file
/home/Hermioner/Documents/test2 is a file
/home/Hermioner/Documents/testfile is a file
[Hermioner@localhost Documents]$
note: for命令用空格来划分列表中的每个值。如果在单独的数据值中有空格,就必须用双引号将这些值圈起来。当然,for的格式也可以是C语言风格的,但是容易搞混。
5. while用法
while命令格式如下:
while test command
do
other command
done
while命令同别的编程语言差不多,只是语法格式不一样而已。并且在test command的使用上,需要加上方括号。参考九九乘法表的例子就行。
note:其实这个例子还可以使用别的循环语句或者条件语句执行。大家可以尝试下。我将在后面的博客中继续总结别的语法。
结束语:第一篇博客确实好难写啊,写了几个小时。。。。。不过确实能学到东西。希望接下来越来越顺利。加油学习吧!!
参考文献:
Linux命令行与shell脚本编程大全(第3版)[美] 布鲁姆(Richard Blum),布雷斯纳汉(Christine Bresnahan) 著,门佳,武海峰 译