shell第五次作业(函数和数组)

该博客展示了如何使用Shell脚本编写函数,包括检查参数是否存在、判断位置参数数量、找出两个数字的最大值以及进行整数的加减乘除运算。此外,还增加了参数个数检查和验证参数是否为整数的功能。

函数和数组

1、编写函数,实现打印绿色OK和红色FAILED 判断是否有参数,存在为Ok,不存在为FAILED
2、编写函数,实现判断是否无位置参数,如无参数,提示错误
3、编写函数实现两个数字做为参数,返回最大值
4、编写函数,实现两个整数位参数,计算加减乘除。

1、编写函数,实现打印绿色OK和红色FAILED 判断是否有参数,存在为Ok,不存在为FAILED

[root@wn2 test6]# vim 1.sh
[root@wn2 test6]# cat 1.sh 
#!/bin/bash
###################
#File name:1.sh
#Version:v1.0
#Email:admin@test.come
#Created time:2021-01-22 10:51:52
#Description:
###################
fun (){
  if [ $# -ne 0 ]
  then
    echo -e "\033[32m OK \033[0m"
  else
    echo -e "\033[31m FAILED \033[0m"
  fi
}
read -p "请输入参数:" i
fun $i
[root@wn2 test6]# bash 1.sh 
请输入参数:12
 OK 
[root@wn2 test6]# bash 1.sh 
请输入参数:
 FAILED 

2、编写函数,实现判断是否无位置参数,如无参数,提示错误

[root@wn2 test6]# vim 2.sh
[root@wn2 test6]# cat 2.sh 
#!/bin/bash
###################
#File name:2.sh
#Version:v1.0
#Email:admin@test.come
#Created time:2021-01-22 11:05:41
#Description:
###################
fun() {
  if [ $# -eq 0 ]
  then
    echo "无位置参数"
  else
    echo "位置参数为$@"
  fi
}
read -p "请输入:" i
fun $i
[root@wn2 test6]# bash 2.sh 
请输入:21 43 54
位置参数为21 43 54
[root@wn2 test6]# bash 2.sh 
请输入:
无位置参数

3、编写函数实现两个数字做为参数,返回最大值

[root@wn2 test6]# vim 3.sh
[root@wn2 test6]# cat 3.sh 
#!/bin/bash
###################
#File name:3.sh
#Version:v1.0
#Email:admin@test.come
#Created time:2021-01-22 11:20:56
#Description:
###################
fun() {
  if [ $a -gt $b ]
  then
    echo "最大值为:$a"
  elif [ $a -eq $b ]
  then
    echo "a=b"
  else
    echo "最大值为:$b"
  fi
}
read -p "please input two numbers:" a b
fun $a $b
[root@wn2 test6]# bash 3.sh 
please input two numbers:12 4
最大值为:12
[root@wn2 test6]# bash 3.sh 
please input two numbers:4 5
最大值为:5
[root@wn2 test6]# bash 3.sh 
please input two numbers:4 4
a=b

4、编写函数,实现两个整数位参数,计算加减乘除。

[root@wn2 test6]# vim 4.sh
[root@wn2 test6]# cat 4.sh
#!/bin/bash
###################
#File name:4.sh
#Version:v1.0
#Email:admin@test.come
#Created time:2021-01-22 11:26:35
#Description:
###################
fun() {
  echo a+b=$((a+b))
  echo a-b=$((a-b))
  echo a*b=$((a*b))
  echo a/b=$((a/b))
}
read -p "请输入两个整数位参数:" a b
fun $a $b
[root@wn2 test6]# bash 4.sh 
请输入两个整数位参数:12 4
a+b=16
a-b=8
a*b=48
a/b=3

如果加上参数个数及参数是否为整数,则shell脚本如下:

[root@wn2 test6]# vim 4.sh
[root@wn2 test6]# cat 4.sh 
#!/bin/bash
###################
#File name:4.sh
#Version:v1.0
#Email:admin@test.come
#Created time:2021-01-22 11:26:35
#Description:
###################
fun() {
  if [ $# -eq 2 ]
  then
    if [[ "$a" =~ ^[0-9]*$ && "$b" =~ ^[0-9]*$ ]]
    then
      echo a+b=$((a+b))
      echo a-b=$((a-b))
      echo a*b=$((a*b))
      echo a/b=$((a/b))
    else
      echo "两个参数不都为整数"
      exit 0
    fi
  else
    echo "参数不为两个"
    exit 0
  fi
}
read -p "请输入两个整数位参数:" a b
fun $a $b
[root@wn2 test6]# bash 4.sh
请输入两个整数位参数:14 2
a+b=16
a-b=12
a*b=28
a/b=7
[root@wn2 test6]# bash 4.sh
请输入两个整数位参数:i 2
两个参数不都为整数
[root@wn2 test6]# bash 4.sh
请输入两个整数位参数:12 3 4
参数不为两个
### 如何在函数中传递数组 在编程中,不同语言有不同的方式来处理数组作为函数参数的情况。以下是几种常见编程语言中关于如何在函数中传递数组的方法。 #### Shell 脚本中的数组传递 在 Shell 中可以通过特殊的方式将数组传递给函数。通过 `$@` 或者 `${array[@]}` 可以将整个数组的内容展开并传入函数内部[^1]。下面是一个基于冒泡排序的例子: ```bash #!/bin/bash bubble_sort() { local -n arr=$1 # 创建一个局部变量arr指向外部数组 n=${#arr[@]} for ((i = 0; i < n-1; i++)); do for ((j = 0; j < n-i-1; j++)); do if [[ ${arr[j]} -gt ${arr[j+1]} ]]; then # Swap elements tmp=${arr[j]} arr[j]=${arr[j+1]} arr[j+1]=$tmp fi done done } # 定义数组 my_array=(5 3 8 6 2) # 将数组传递给函数 bubble_sort my_array echo "${my_array[@]}" ``` 此脚本展示了如何使用 `local -n` 来创建一个名称引用类型的变量,从而允许修改原始数组内容[^2]。 #### C/C++ 中的数组传递 C C++ 提供了多种机制用于处理数组作为函数参数的情形。一种常见的做法是指针加长度的形式,另一种则是直接指定数组名作为指针形式。推荐的做法是显式声明数组大小以及其元素数量以便于操作维护安全性[^3]。 以下是一段典型的例子展示如何打印整型数组的所有元素: ```c++ #include <iostream> void printArray(int a[], int size) { for (int i = 0; i < size; ++i){ std::cout << a[i] << ' '; } std::cout << '\n'; } int main(){ const int SIZE = 5; int numbers[SIZE] = {1, 2, 3, 4, 5}; printArray(numbers, SIZE); } ``` 值得注意的是,在上述代码片段里,虽然我们表面上只提供了数组的名字而未提及具体地址信息,但实际上编译器会自动将其转换成指向第一个元素的指针。 #### 字符串数组排序实例(C) 对于更复杂的场景比如字符串数组排序,则可以采用类似的思路构建相应的算法逻辑。这里给出一段基于字符数组实现字符串升序排列的小程序[^4]: ```c #include<stdio.h> #include<string.h> #define MAX_LEN 100 // 子函数:对字符串进行排序 void strSort(char st[]) { int l = strlen(st), temp; char t; for(int k=0;k<l-1;k++) // 外层循环控制轮数 for(int i=0;i<l-k-1;i++) // 内层循环逐比较相邻项 if(st[i]>st[i+1]){ t=st[i]; st[i]=st[i+1]; st[i+1]=t; } } int main(){ char s[MAX_LEN]; printf("Enter string:"); scanf("%s", &s); strSort(s); printf("Sorted String:%s\n",s); return 0; } ``` 以上各部分分别介绍了三种主流编程环境下有关“如何在函数间传输数组”的解决方案及其实际应用案例分析。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值