未加星标

Use two configuration variables in bash script

字体大小 | |
[系统(linux) 所属分类 系统(linux) | 发布者 店小二04 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

I am trying to create a shell script that will keep the last n files in a directory and delete the rest. The config file that I am using is as under:

#config dir1="/home/user/test" n1="2" dir2="/home/user/test/temp" n2="3"

What I intend to achieve is goto dir1 and keep the last n1 files. I am testing out the following code

source /home/cfolder/config ls -t $dir1 | sed -e '1,'$n1'd' | xargs -d '\n' rm

How do I get the code to loop through all the config parameters without explicitly writing a line of code for each dir and n group?

I do not know exactly how to do what you want with the way your problem is set up. However, here is another approach you can use to this kind of problem.

For separate configurations, I like to have config files named X, Y, or Z.conf (of course, replace X, Y, and Z with meaningful names).

In these .conf files, use dir and n instead of dir1 and n1.

#X.conf dir="/home/user/test" n="2"

and

#Y.conf dir="/home/user/test/temp" n="3"

Then, in a bash script, I will do:

# runconfigs.sh for config in ./*.conf; do <your_script_name_goes_here>.sh "$config" done

In your script, you can use something like

#yourscript.sh source "$1" ls -t $dir | sed -e '1,'$n'd' | xargs -d '\n' rm

Then, you can simply run ./runconfigs.sh . I find that this approach becomes more useful when you're dealing with many configurations, but I don't see why it isn't also applicable here.

本文系统(linux)相关术语:linux系统 鸟哥的linux私房菜 linux命令大全 linux操作系统

分页:12
转载请注明
本文标题:Use two configuration variables in bash script
本站链接:https://www.codesec.net/view/620808.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(linux) | 评论(0) | 阅读(11)