以下是我的/ var / www / apache2 / log文件夹中的日志示例 – ./no_domain_access.log.7.gz
./no_domain_access.log.8.gz
./no_domain_access.log.9.gz
./no_domain_error.log.10.gz
./no_domain_error.log.11.gz
./no_domain_error.log.12.gz
./no_domain_error.log.13.gz
./no_domain_error.log.14.gz
./no_domain_error.log.15.gz
./no_domain_error.log.16.gz
./no_domain_error.log.17.gz
./no_domain_error.log.18.gz
./no_...
我发现我正在编写很多可以使用n元组列表清理的Makefile.但是我找不到任何方法(干净利落地)做到这一点.到目前为止,我只能使用$(shell …)和tr,sed或其他非Makefile标准.
例如,我想这样做:XYZs = dog.c pull_tail bark duck.c chase quack cow.c tip mooall:@- $(foreach X Y Z,$(XYZs), $(CC) $X -o bully/$Y ; ln bully/$Y sounds/$Z ; )有没有一种在Makefile中迭代n元组列表的好方...
以下命令尝试枚举当前目录中的所有* .txt文件并逐个处理它们:for line in "find . -iname '*.txt'"; do echo $linels -l $line;
done为什么我会收到以下错误?:ls: invalid option -- 'e'
Try `ls --help' for more information.解决方法:这是循环文件的更好方法,因为它处理文件名中的空格和换行符:find . -type f -iname "*.txt" -print0 | while IFS= read -r -d $'\0' line; doecho "$line"ls -l "$line"
done
我有一个bash脚本,我需要遍历find命令的输出的每一行,但看起来我正在从find命令迭代每个Word(空格分隔).到目前为止我的脚本看起来像这样:folders=`find -maxdepth 1 -type d`for $i in $folders
doecho $i
done我希望这会给出如下输出:./dir1 and foo
./dir2 and bar
./dir3 and baz但我得到这样的输出:./dir1
and
foo
./dir2
and
bar
./dir3
and
baz我在这做错了什么?解决方法:由于您没有使用find的任何更高级功能,因此可以使...
我想迭代一个文件列表.这个列表是find命令的结果,所以我想出了:getlist() {for f in $(find . -iname "foo*")doecho "File found: $f"# do something usefuldone
}没关系,除非文件名中有空格:$ls
foo_bar_baz.txt
foo bar baz.txt$getlist
File found: foo_bar_baz.txt
File found: foo
File found: bar
File found: baz.txt我该怎么做才能避免空格分裂?解决方法:您可以使用基于行的迭代替换基于单词的迭代:find . -iname "foo...
假设我有2个单独的循环for file1 in `ls Dir1/` ; doecho $file1
donefor file2 in `ls Dir2/` ; doecho $file2
done我想单循环迭代两个目录伪代码for file1 , file2 in `ls Dir1` , `ls Dir2`
doecho $file1echo file2
done可能吗解决方法:假设目录列表中没有IFS阻止字符,具有合适输入的while循环可以完成这项工作.paste <(ls /var) <(ls /usr) | while read e u; do echo $e $u; done
我正在学习一系列教程来学习Bash shell脚本.其中一个练习是遍历当前目录中的文件并在这些文件中搜索模式.如果找到模式,那么脚本应该总结这些文件的文件大小.#!/bin/sh
patern=echo
totalSize=0for file in *
do[ ! -f $file ] && continue if grep $patern $file > /dev/nullthen echo "pattern matched in $file" echo "file size is `stat -c%s $file`"fileSize=`stat -c%s $file`totalSize=`expr $totalSize + $fileSize`e...
我有一个超过400个坐标,我想作为参数传递给一个字符串,但我不知道如何将第一个参数作为“lat”传递,第二个参数传递为“lng”,依此类推.
说我过去了./test 1 2 3 4我想要我的输出coordinate: {lat: 1, lng: 2}
coordinate: {lat: 3, lng: 4}这是我到目前为止所做的,但显然这不是它的完成方式.for i in $@
doecho "coordinate: {lat: $i, lng: $i}"done解决方法:
#!/usr/bin/env bash
while (( "$#" >= 2 )); doecho "coordinate: {l...
我有两个文件.
file.txt和delete.txt
file.txt包含以下内容:/somedirectory/
/somedirectory2/somefile.txt
/anotherfile.txtdelete.txt包含:/somedirectory/
/somedirectory2/somefile.txt我需要删除delete.txt中包含的file.txt中的行
cat file.txt应该导致:/anotherfile.txt到目前为止,我已经尝试过这个没有运气:while read p; doline=$p;sed -i '/${line}/d' file.txt;
done < delete.txt我没有收到任何错误,它只是不编辑我...
如果我在终端中键入echo $PATH,我会得到以下结果:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/home/fnorbert/.local/bin:/home/fnorbert/bin我想使用shell脚本迭代这些路径,但我不知道如何做到这一点.我尝试了以下方法:for i in 1 2 3
doecho $PATH | cut -d':' -f$i
done这将打印前三个路径,但是如果可能的话,我想用变量i表示每个路径.解决方法:您可以将read with delimiter设置为:while read -d ':' p; doecho "$p...