操作系统与Linux 应用

编写一个名为yjx_multipleof7的脚本程序,使用位置参数进行参数传递。参数为一小于100的正整数。先检查位置参数是否符合要求。如果不符合要求,请给出提示;如果符合要求,将1~ 该正整数之内的所有7的倍数输出或输出“1~ 该正整数之内不存在7的倍数”。

img

img

1、

if test -d "/path/to/file"
then
  # 询问用户输入n的数值
  read -p "Enter the value of n: " n
  # 在/path/to/file目录下创建多个文件
  for i in $(seq 1 $n)
  do
    touch "/path/to/file/zsfxx$i"
  done
fi

2、

#!/bin/bash

# 判断参数是否符合要求
if ! [[ $1 =~ ^[0-9]+$ ]] || [ $1 -ge 100 ]
then
  # 不符合要求,给出提示
  echo "Error: invalid argument"
  exit 1
fi

# 将1~该正整数之内的所有7的倍数输出
for i in $(seq 1 $1)
do
  if [ $((i % 7)) -eq 0 ]
  then
    echo $i
  fi
done