我的脚本是rm命令的模仿,长话短说。谁能指出我的删除脚本中存在的错误/不必要的行,这些错误/不必要的行导致我的输出产生过多/无关的行?该代码按预期工作,但是它会产生所有这些不必要/过多/重复的行。当我尝试删除同一行中的2个文件并执行一些其他简单命令时,下面的输出是什么样的。先感谢您。感谢您的帮助。
输入:
'{path}/**/*.pdf'.format(path=path1)
like:
glob.glob('{path}/**/*.pdf'.format(path=path),recursive=True)
输出:
import java.util.ArrayList;
import java.util.Iterator;
public class TestClass {
private int value;
private int lowestValue;
TestClass() {
//default constructor
}
TestClass(int value) {
this.value = value;
}
TestClass(int value, int lowestValue) {
this.value = value;
this.lowestValue = lowestValue;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getLowestValue() {
return lowestValue;
}
public void setLowestValue(int lowestValue) {
this.lowestValue = lowestValue;
}
public void printTestClass() {
System.out.println("For this item, value = " + this.getValue() + " and lowestValue = " + this.getLowestValue());
}
public static void main(String args[]) {
ArrayList < TestClass > al = new ArrayList < TestClass > ();
int temp = 0;
int[] intArray = new int[] {
3,
2,
1,
3,
2,
3
};
//create list
for (int c: intArray) {
TestClass tc = new TestClass(c);
al.add(tc);
}
int nextIndexToSet = 0; //where we set lowest value
boolean firstValueInThisRun = true; //nothing to compare to preceding it
// process
for (int i = 0; i < al.size(); i++) {
if (firstValueInThisRun) {
temp = al.get(i).getValue();
firstValueInThisRun = false;
continue; //because we could not possibly have an increment yet
}
//still here, check for an increment
if (temp >= al.get(i).getValue()) {
temp = al.get(i).getValue(); //set to the new lower value, or to itself
continue; //because no increment found so nothing more to do yet
}
//still here, then there was an increment
for (int j = nextIndexToSet; j < i; j++) {
TestClass tc = new TestClass(al.get(j).getValue(), temp);
al.set(j, tc);
System.out.println("j = " + j + " i = " + i);
}
nextIndexToSet = i;
firstValueInThisRun = true;
} //closes outer for
//check if anything still unset
if (nextIndexToSet < al.size()) {
for (int j = nextIndexToSet; j < al.size(); j++) {
TestClass tc = new TestClass(al.get(j).getValue(), al.get(al.size() - 1).getValue());
al.set(j, tc);
}
}
//print the array list to make sure
Iterator < TestClass > al_Iter = al.iterator();
System.out.println("Elements in array list are : ");
while (al_Iter.hasNext()) {
al_Iter.next().printTestClass();
}
}
}
sh remove file2 file4
答案 0 :(得分:1)
for i in $*
应该是for i in "$@"
或简单地是for i
。"$1"
,"$i"
,
"$filename"
,
"$verbose"
等)$[expression]
已过时。
使用$((expression))
。您的主循环调用
delete_file $filename
(第100行)。
您的delete_file
功能集
filename=$1
(第35行), 这有点多余,因此令人困惑。
baseline
,但从未使用过。
您无需设置即可测试(即reference)$basefule
。
这些是相同的变量吗?代码说
if [ ! -f ~/project ]
then
echo "cannot remove $filename: no such file or directory"
︙
这是 非常 误导性消息。
M A I N
”,
但是“主要”代码大约提前33行开始。for i in $* do filename=$i # This is an example of terrible indenting. ︙ delete_file $filename ︙ done,但是随后五行,
delete_file $*因此您要处理两次文件。 因此,即使
delete_file
首次调用成功,
再次调用该文件时,该文件将消失。delete_file
)
加上脚本的所有参数,
您应该使用"$@"
而不是$*
。delete_file
,
那么delete_file
需要迭代(循环)这些参数。
您的delete_file
函数仅查看$1
。