linux - 如何创建能够让我查看、修改和附加它创建的文本文件的 Shell 脚本

标签 linux bash

我需要创建一个 shell 脚本:

  • 可以编写 shell 应用程序来创建文件。

  • 能够查看、修改和追加文件

任何帮助将不胜感激。因为我找不到任何有帮助的资源。

最佳答案

这是一个帮助您入门的脚本(未经测试):

file="$HOME/my_journal_$$"
printf "Enter what you want to do ([v]iew, [m]odify, or [a]ppend): "
read rsp
case $rsp in
    v* ) cat "$file" ;;
    m* ) vi "$file" ;;
    a* ) printf 'appended this @ %s\n' "$(date)" >> "$file" ;;
    * )  printf 'bad response, quitting\n' >&2 ;;
esac

要使其健壮、灵活,您仍然有很多工作要做,在调用之间使用相同的真实文本文件,收集测试以附加,循环直到获得良好的响应而不是现有的响应,添加一个 shebang,也许使用 getopts 进行 arg 解析等...但这只是一个开始。祝你好运!

关于linux - 如何创建能够让我查看、修改和附加它创建的文本文件的 Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55945187/

相关文章:

windows - Gitlab CI : How to use the bash shell on a Windows runner

ruby - 自动上传 xmodem 文件

linux - 在 bash 命令中调用自定义函数

linux - 如何每 4 小时 48 分钟安排一次 cron 作业?

linux - mount:/dev/mapper/centos-home: 无法读取 super block

linux - lsb_release : command not found in latest Ubuntu Docker container

c - 关闭 Linux 套接字后奇怪的程序行为

linux - 如何删除包含匹配单词的文件中给定间隔的行?

linux - 在 Nagios 中创建事件

bash - 无法启动 docker nginx