我如何在 bash 中编写 for 循环?

我如何在 bash 中编写 for 循环?

我想要打开manoj.log位于以下路径下所有文件夹中的文件manoj/version_2019_logs/

log_Job_1062240383596339132  log_Job_211734815538351099   log_Job_3169874810010948270  log_Job_4892521732453090370  log_Job_616017367232222009   log_Job_7746050547434505746
log_Job_1087552901530027532  log_Job_2138986249704467409  log_Job_3213449139857876800  log_Job_4942467390574829611  log_Job_6238656570813686104  log_Job_7759691180822939673
log_Job_1094023052482863156  log_Job_2182932699028741039  log_Job_3495541071515971436  log_Job_4991262145297430772  log_Job_6249625539763724888  log_Job_7788367464939735256
log_Job_1112824059179562734  log_Job_2207200802159530309  log_Job_3521779894265247240  log_Job_5008465057706583586  log_Job_6332625113887952497  log_Job_7839680850535981076
log_Job_1115961800550360808  log_Job_2228975011057546920  log_Job_3533519667940390181  log_Job_5049358369076182790  log_Job_6361020266613439417  log_Job_7964228434990116606
log_Job_116620716924852324   log_Job_2261899893101289922  log_Job_3555258833489657222  log_Job_5070302167247818598  log_Job_6365357993168094239  log_Job_8002059843324192384
log_Job_1170879160927751773  log_Job_2272498444342380460  log_Job_3600396812903363880  log_Job_5074230589225850269  log_Job_6368737841043712206  log_Job_8061631342059238735
log_Job_1196408618998724690  log_Job_2330347127867365984  log_Job_3634398724791103066  log_Job_5104135853687112914  log_Job_6403065722729148627  log_Job_8107172303042965189
log_Job_119679766735713912   log_Job_2355390575490678881  log_Job_3672868486994968310  log_Job_5234694258772377321  log_Job_6471963239355938981  log_Job_823953756655189469
log_Job_1231649352430356213  log_Job_2411194519887946249  log_Job_3744881615111906696  log_Job_5265135194070264105  log_Job_6647274930071248190  log_Job_8250496313145841054
log_Job_1291462851629031888  log_Job_2447073498264497344  log_Job_3865452807586800686  log_Job_5276193961228324010  log_Job_6723119902297861581  log_Job_8277582748108036958
log_Job_1311009744768855803  log_Job_2483079159144617059  log_Job_3865756556434808301  log_Job_5285727775355309830  log_Job_6823765721931003259  log_Job_8291284541813195543
log_Job_1360314167599818954  log_Job_2501619386435329867  log_Job_3947659835596363547  log_Job_5290206280084030158  log_Job_6889161829798138272  log_Job_8336408158149543171
log_Job_1405292115798715774  log_Job_2524010021812014662  log_Job_3979236574680951324  log_Job_5331896359662112548  log_Job_7074944864832685338  log_Job_8347075046865033230
log_Job_1405828172132051268  log_Job_2543166016332836099  log_Job_4045632782395406038  log_Job_5378157440012355550  log_Job_7261690464853350955  log_Job_8564429729044922796
log_Job_1464702800465643092  log_Job_2572751888641064056  log_Job_4135811427158656535  log_Job_5390716124504864185  log_Job_7287505928902866820  log_Job_8568662967470489510
log_Job_1471130387126560083  log_Job_2668458088273847924  log_Job_4147963044605392650  log_Job_5408361634344986148  log_Job_7304175529688633736  log_Job_8613196494055930615
log_Job_1478268927477655562  log_Job_2698537704021630704  log_Job_4175120655058738150  log_Job_541722979830226836   log_Job_7359150622965665760  log_Job_862668550089968842
log_Job_1508196714417882853  log_Job_2792933242406042283  log_Job_4180433154273265198  log_Job_5454540177109190256  log_Job_7377174057934272358  log_Job_8660494343243708448
log_Job_1549360820432609844  log_Job_2817457208514922684  log_Job_419407952946841507   log_Job_5456298767627397963  log_Job_740421902971615066   log_Job_8715244862923908609
log_Job_1553776244108482157  log_Job_2859448491921143532  log_Job_4273406655134441797  log_Job_5547680114806486858  log_Job_7411417155483981582  log_Job_871677488713145443
log_Job_1613216086590722773  log_Job_2879675772504618624  log_Job_4340503663139348627  log_Job_5557217089856980750  log_Job_7422301003357573850  log_Job_8724270883508614287
log_Job_165770286603004604   log_Job_2915245634483392662  log_Job_4416933572751204923  log_Job_562892198056432639   log_Job_7457327878103289840  log_Job_8794914526350339174
log_Job_1702430511900073891  log_Job_2941480918319839789  log_Job_4466704897272239662  log_Job_5638976205162749642  log_Job_7492261438775554860  log_Job_8851345829377485305
log_Job_1747867558642274950  log_Job_2988182773019854646  log_Job_4500099793919329764  log_Job_5703166485155140764  log_Job_7554270113838601502  log_Job_9005290733862815526
log_Job_1767233114324533459  log_Job_2992801188516322455  log_Job_453789638827411661   log_Job_5888383456653650932  log_Job_7557467681433021278  log_Job_9059217454636868602
log_Job_1785465391229569910  log_Job_3005325732554887205  log_Job_4585196025191111673  log_Job_5963378021843985018  log_Job_7575119799454867909  log_Job_9092041077708299542
log_Job_1887921616008501391  log_Job_3023775660571552706  log_Job_468600602363359471   log_Job_5981170779176882904  log_Job_7580598741511229107  log_Job_9105134874285877652
log_Job_1929432448277805907  log_Job_3061521921553431743  log_Job_4772411242598498525  log_Job_6094794862421668499  log_Job_7669172638918797906  log_Job_98769170566399869
log_Job_1936750047710684959  log_Job_3070842610659246026  log_Job_4802442976771332117  log_Job_6102704687818984513  log_Job_769643235239940935   log_Job_990241390610008625
log_Job_2080327063817782296  log_Job_3143694026941472975  log_Job_48812702431965961    log_Job_613442165543596837   log_Job_7699220604840056998  packstarter.out
log_Job_2110297521091118790  log_Job_3165456404749168712  log_Job_489182359373176073   log_Job_6151264531595272977  log_Job_7702260850557985265

我想做的事:

进入每个log_job_*文件夹,cat文件并执行如下操作。

#!/bin/bash

cd manoj/version_2019_logs/

for file in /log_Job_*/manoj.log



 do 

        cat $file

        #============ While reading line by line, match the given strings and store them into variables================

        ##############################TOTAL DURATION#####################
        {
        TDS="$(grep 'Logfile started' manoj.log |  awk '{print $3,$4}' | cut -d: -f2-)"


        TDE="$(grep 'Process removed' manoj.log | awk '{print $1,$2}' | cut -d -f1-)"
         # Convert irregular date to yyyy-mm-dd hh:mm:ss format
        convert_date(){ printf '%s-%s-%s %s' ${1:6:4} ${1:3:2} ${1:0:2} ${1:11:8}; }

        # Convert to timestamp (time is seconds)
        TDS_TS=$(date -d "$(convert_date "$TDS")" +%s)
        TDE_TS=$(date -d "$(convert_date "$TDE")" +%s)

        # Subtract
        TotalDuration=$((TDE_TS-TDS_TS))
        # convert to HH:MM:SS (note, that if it'sd more than one day, it will be wrong!)
        date -d "@$TotalDuration" +%H:%M:%S
        }
    done

出现错误:

   cat: /log_Job_*/manoj.log: No such file or directory
-bash: While: command not found
grep: manoj.log: No such file or directory
grep: manoj.log: No such file or directory

我对编码还很陌生。有人能帮我解决这个问题吗?

答案1

删除前导的/,因为这意味着您从文件系统根目录 ( ) 开始搜索具有绝对路径的文件,而不是从您刚刚输入的/相对路径( ) 开始搜索。cdmanoj/version_2019_logs

另外,你说你想打开manoj.log,但是告诉for循环打开process.log

您应该使用目录的绝对路径manoj/...

尝试这个,

#!/bin/bash
cd /path/to/manoj/version_2019_logs/
for file in log_Job_*/manoj.log; do
    grep 'Logfile started' "$file"
    ...
done

相关内容