[英]Bash script to delete backup files within a given directory & calling a calling that script in another method?
I have a test harness called "test.sh" that will create 24 backup files in a directory called "D1" with extensions .txt~, .bak, and 12 files that start with #. 我有一个名为“ test.sh”的测试工具,它将在名为“ D1”的目录中创建扩展名为.txt〜,.bak的24个备份文件,以及以#开头的12个文件。 In this test harness I have to call another method called "clean.sh" that will delete all the files in the given directory (in this case D1).
在这个测试工具中,我必须调用另一个名为“ clean.sh”的方法,该方法将删除给定目录(在本例中为D1)中的所有文件。
This is my test.sh: 这是我的test.sh:
#!/bin/bash
#Create new directory called D1
mkdir D1
#cd into D1
cd ./D1
#Create 12 backup files .bak in D1 (using for loop)
for i in `seq 1 12`;
do
#echo $I
touch file$i.bak D1
done
#Create 12 backup files .txt~ in D1 (using set)
touch {a..l}.txt~ D1
#Create 12 files prefix # in D1 (using while loop)
COUNTER=0
while [ $COUNTER -lt 12 ]; do
touch \#$COUNTER.txt D1
let COUNTER=COUNTER+1
done
#Once finished creating backup files in all 3 categories, do an ls -l
ls -l
#Invoke clean method here
./cleanUp.sh
#Do final ls - l
ls - l
This is clean.sh: 这是clean.sh:
#!/bin/bash
#Need to pass a directory in as argument
for i in "$@"
do
echo file is: $I
done
I'm not sure how to pass in a directory as an argument and calling a method in another method is confusing. 我不确定如何将目录作为参数传递并在另一个方法中调用方法会造成混淆。 Thanks!
谢谢!
Your script is a nice start. 您的脚本是一个不错的开始。
The touch commands can do without the parameter D1, your cleanup.sh call needs a parameter. 触摸命令可以不带参数D1,您的cleanup.sh调用需要一个参数。 The parameter ${PWD} is given by the shell, thats easy in this case.
参数$ {PWD}由外壳程序给出,在这种情况下很简单。 (you can also give a parameter like /home/mina/test2/D1).
(您还可以提供类似/ home / mina / test2 / D1的参数)。
The script will look like 脚本看起来像
!/bin/bash
#Create new directory called D1
mkdir D1
#cd into D1
cd ./D1
#Create 12 backup files .bak in D1 (using for loop)
for i in `seq 1 12`;
do
#echo $I
touch file$i.bak
done
#Create 12 backup files .txt~ in D1 (using set)
touch {a..l}.txt~
#Create 12 files prefix # in D1 (using while loop)
COUNTER=0
while [ $COUNTER -lt 12 ]; do
touch \#$COUNTER.txt
let COUNTER=COUNTER+1
done
#Once finished creating backup files in all 3 categories, do an ls -l
ls -l
#Invoke clean method here
./cleanUp.sh ${PWD}
#Do final ls - l
ls - l
Now the cleanup script. 现在是清理脚本。
First change $I
into $i
, the variable name is case sensitive. 首先将
$I
更改为$i
,变量名称区分大小写。
With for i in "$@"
you loop through the parameters (only the name of the dir), and you would like to go through the diffferent filenames. for i in "$@"
使用for i in "$@"
您可以遍历参数(仅是目录名),并且希望遍历不同的文件名。
Try the following alternatives: 尝试以下替代方法:
# Let Unix expand the * variable
for i in $1/*; do
# Use a subshell
for i in $(ls "$@"); do
# use a while-loop
ls $@ | while read i; do
# use find and while
find "$1" -type f | while read i; do
# use find and -exec
find "$1" -type f -exec echo File is {} \;
# use find and xargs
find "$1" -type f | xargs echo File is
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.