Linux shell脚本编程基础之练习篇

简介:

  shell脚本编程基础之练习篇。

  • 1、编写一个脚本使我们在写一个脚本时自动生成”#!/bin/bash”这一行和注释信息。

复制代码
#!/bin/bash
if [ $# -ne 1 ]
then
        echo "请输入一个参数"
        exit
else
        echo "参数正确"
        newfile=$1
fi

#echo `grep "^#\!" ${newfile}`

if ! grep "^#\!" ${newfile} &>/dev/null
then
cat >>${newfile}<<EOF
#!/bin/bash
# Author: Inert Your Name here.
#Date & Time: `date +"%F %T"`
#Description: Please Edit here.
EOF
fi
vi +5 ${newfile}
复制代码

 将脚本改个名字例如:newshfile,将其放置在/bin/目录下,那么你的系统就多了一个新的newshfile命令了

  • 2、求100以内偶数的和

复制代码
#!/bin/bash
# Author: Inert Your Name here.
#Date & Time: 2015-06-02 20:37:07
#Description: Please Edit here.
let sum=0
for index in {1..100}
do
if [ $[ ${index}%2 ] == 0 ]; then
        #let sum+=${index}
        sum=`expr ${sum} + ${index}`
fi
done
echo "sum=${sum}"

let sum=0
for num in $(seq 1 100); do
if [ $[ $num % 2 ] == 0 ]; then
        sum=`expr $sum + $num`
fi
done
echo "sum=$sum"
复制代码
  • 判断输入的参数个数,如果为两个参数则相加并输出相加后的值
复制代码
#!/bin/bash
if [ $# -eq 2 ]
then
        echo "参数个数 $#\n"
        echo "参数相加 $1 + $2 = `expr $1 + $2`"
else
        echo "参数个为 $#,本脚本需要两个参数" 
fi
复制代码
  • 用while\for循环降序输出1~5的值
复制代码
#!/bin/sh
num=5
while test $num != 0
do
        echo "$num"
        num=`expr $num - 1`
done

echo "*****************************"
num=5
while (($num != 0))
do
        echo "$num"
        num=`expr $num - 1`
done


echo "*****************************"
for num in {5..1}
do
        echo "$num"
done

echo "*****************************"
for ((num=5;$num>0;num=`expr $num - 1`))
do
        echo "$num"
done
复制代码
  •  加减乘除运算
复制代码
#!/bin/bash
# Author: Inert Your Name here.
#Date & Time: 2015-06-02 21:32:51
#Description: Please Edit here.
if test $# == 3
then
        echo "参数个数$#,参数:$@"
case $1 in
        +)
        num=`expr $2 + $3`
        ;;
        -)
        num=`expr $2 - $3`
        ;;
        x)
        num=`expr $2 \* $3`
        ;;
        /)
        num=`expr $2 \/ $3`
        ;;
        *)
        echo "只允许+ - x /这几个运算符"
        ;;
esac
echo "num=$num"
else
        echo "参数个数为3个,分别为\"运算符 参数1 参数2\""
fi
复制代码
  • 浮点数的运算

echo 5.12 + 2.5 | bc

复制代码
#!/bin/bash
# Author: Inert Your Name here.
#Date & Time: 2015-06-02 22:07:28
#Description: Please Edit here.

a=5.66
b=8.67
c=`echo $a + $b | bc`
echo "$a + $b = $c"
复制代码
  • 打印以下各种图形效果
复制代码
1
22
333
4444
55555
****************************
1
12
123
1234
12345
****************************
|_
||_
|||_
||||_
|||||_
****************************
 *
 * *
 * * *
 * * * *
 * * * * *
 * * * * *
 * * * *
 * * *
 * *
 *
************lengxing****************
      *
     * *
    * * *
   * * * *
  * * * * *
 * * * * * *
  * * * * *
   * * * *
    * * *
     * *
      *
复制代码
复制代码
#!/bin/bash
# Author: Inert Your Name here.
#Date & Time: 2015-06-02 22:24:41
#Description: Please Edit here.

for ((num=1;$num<=5;num=`expr $num + 1`))
do
        for((index=$num;$index>0;index=`expr $index - 1`))
        do
                echo -n "$num"
        done 
echo ""
done 

echo "****************************"
for ((num=1;$num<=5;num=`expr $num + 1`))
do
        for((index=1;$index<=$num;index=`expr $index + 1`))
        do
                echo -n "$index"
        done
echo ""
done

echo "****************************"
for ((num=1;$num<=5;num=`expr $num + 1`))
do
        for((index=1;$index<=$num;index=`expr $index + 1`))
        do
                if [ $index%2 == 0 ]; then
                        echo -n " "
                else
                        echo -n "|"
                fi
        done
echo -n "_"
echo ""
done

echo "****************************"
for (( i=1; i<=5; i++ ))
do
    for (( j=1; j<=i;  j++ ))
    do
     echo -n " *"
    done
    echo ""
done

for (( i=5; i>=1; i-- ))
do
    for (( j=1; j<=i;  j++ ))
    do
     echo -n " *"
    done
    echo ""
done

echo "************lengxing****************"
max=6
for ((i=1; i<=$max; i++))
do
        for ((j=$max-i; j>0; j--))
        do
                echo -n " "
        done
        for ((k=1; k<=i; k++))
        do
                echo -n " *"
        done
        echo ""
done

for ((i=1; i<=$max; i++))
do
        for ((k=1; k<=i; k++))
        do
                echo -n " "
        done
        for ((j=$max-i; j>0; j--))
        do
                echo -n " *"
        done
        echo ""
done
复制代码





本文转自秋楓博客园博客,原文链接:http://www.cnblogs.com/rwxwsblog/p/4547589.html,如需转载请自行联系原作者
目录
相关文章
|
6天前
|
存储 Shell Linux
Linux Bash 脚本中的 IFS 是什么?
【4月更文挑战第25天】
14 0
Linux Bash 脚本中的 IFS 是什么?
|
16小时前
|
存储 Shell C语言
shell脚本 编程 变量 基本入门(详解)
shell脚本 编程 变量 基本入门(详解)
|
18小时前
|
Linux Shell 程序员
【Linux】权限(shell运行原理、概念,Linux权限)
【Linux】权限(shell运行原理、概念,Linux权限)
6 2
|
1天前
|
存储 运维 Java
Linux笔记02 —— Shell补充
Linux笔记02 —— Shell补充
19 2
|
1天前
|
安全 Linux Shell
Linux笔记01 —— Linux初识与Shell汇总(请配合另一篇《Linux笔记02》一起使用)
Linux笔记01 —— Linux初识与Shell汇总(请配合另一篇《Linux笔记02》一起使用)
9 1
|
1天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
8 3
|
6天前
|
弹性计算 运维 监控
|
6天前
|
存储 弹性计算 运维
自动化收集员工信息的Shell脚本
【4月更文挑战第30天】
10 0
|
7天前
|
弹性计算 运维 Shell
使用shell 脚本打印图形3
【4月更文挑战第29天】
18 0
|
7天前
|
存储 弹性计算 运维
使用shell 脚本打印图形2
【4月更文挑战第29天】
17 0