shell作业三

简介: shell作业三

shell作业

1.你需要打印一个给定的数字的反序,如输入10572,输出27501,如果没有输入数据,应该抛出错误和使用脚本说明。

#!/bin/bash
if [ $# -eq 0 ]; then
    echo "错误:未提供输入数据!"
    echo "用法:$0 <number>"
    exit 1
fi
num=$1
reverse=()
while [ $num -gt 0 ]
do
    remainder=$(( $num % 10 ))
    reverse+=($remainder)
    num=$(( $num / 10 ))
done
echo "${reverse[*]}" | tr -d ' '

2.写出SHELL函数RevertInput,函数必须获取三个参数,然后将三个参数倒序echo打印出来,函数必须检查参数个数的合法性,如果参数非法,打印”Illegal parameters”,对于下面的输入:

RevertInput “this is para1” para2 para3

应该输出:

para3

para2

this is para1(注:需要注意第一个参数中的空格)

#!/bin/bash
RevertInput() {
    if [ $# -ne 3 ]; then
        echo "Illegal parameters"
        return
    fi
    arr=("$3" "$2" "$1")
    echo "${arr[@]}"
}
RevertInput "$1" "$2" "$3"


相关文章
|
5月前
|
Shell
shell作业项目留存代码
shell作业项目留存代码
38 1
|
Shell
shell作业(一)
shell作业(一)
|
Shell
shell作业(二)
shell作业(二)