shell脚本练习:每天一个shell脚本(7)

我的博客都是依照自己浅薄的现有知识去写的,好多时候写完了博客,去查找答案,会发现很多更剪短更效率更好的答案。写博客是激励我自己能坚持下去,让我的头脑不去懈怠,望共勉。

题目:

Write a script five_dirs.sh that does these tasks:
– make a directory five
– make five subdirectories five/dir1 through five/dir5
– in each subdirectory, make four files, file1 through file4, such that file1 has one line
containing the digit 1, file2 has two lines, each containing the digit 2, …, and file4
has four lines, each containing the digit 4

涉及知识点:
1.创建文件夹

mkdir  dirname

2.创建文件

touch filename

3.文件内容写入

echo content

4.for 循环 seq 使用

for i in `seq 5`
do
done

我自己的解决办法:

#!/bin/bash
mkdir five
cd five
for i in `seq 5`
do
    mkdir${i}
    for j in `seq 4`
    do
        touch ${PWD}'/dir'${i}'/file'${j}
        for k in `seq $j`
        do
            echo $j >> ${PWD}'/dir'${i}'/file'${j}
        done
    done
done

猜你喜欢

转载自blog.csdn.net/weixin_43490690/article/details/89465964