shell Bash脚本未生成包含所需内容的csv文件[已关闭]

ghg1uchk  于 2023-05-01  发布在  Shell
关注(0)|答案(1)|浏览(118)

**关闭。**此题需要debugging details。目前不接受答复。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
3天前关闭。
Improve this question
我在linux环境下运行下面的脚本。

#!/bin/bash

export P4PORT="perforceserver:port"
export P4USER="user"
export P4PASSWD=""

# Set the directories to compare
dir1="/home/user1/code1/"
dir2="/home/user1/code2/"

# Set the CSV file to save the comparison results
csvfile="comparison.csv"

# Compare the directories and save the results in the CSV file
diff -rq $dir1 $dir2 | sed 's/: /\t/g' | while read line
do
    file=$(echo $line | awk '{print $2}')
    submitter=$(p4 describe -s $file | awk '/^Change/ {split($0, a, " "); print a[3]}; /^User/ {split($0, a, " "); print a[2]}')
    echo "$line $submitter" >> $csvfile
done

dir2 - data downloaded from Perforce VCS
脚本上面的问题没有将Perforce提交的所有者信息写入csv文件。
示例预期输出:-

Type    code1  code2   Differ  PerforceOwner 
 Files      A.c   A.c      differ  Daniel.u
 Files       b.c   b.c     differ  venket.k

请帮我解决上面的问题

o3imoua4

o3imoua41#

第17行中的awk使用white space作为分隔符,而不仅仅是tab。因此,内容将不包含预期的文件名,$2将停止在下一个“白色”。
所以一个diff结果像

diff -rq "$dir1" "$dir2"
Only in /home/user1/code1/: file name

将由您的脚本处理为结果

echo $file
in

以下是更正后的脚本:

#!/bin/bash

export P4PORT="perforceserver:port"
export P4USER="user"
export P4PASSWD=""

# Set the directories to compare
dir1="/home/user1/code1/"
dir2="/home/user1/code2/"

# Set the CSV file to save the comparison results
csvfile="comparison.csv"

# Compare the directories and save the results in the CSV file
diff -rq "$dir1" "$dir2" | sed 's/: /\t/g' | while read line
do
    file=$(echo "$line" | awk 'BEGIN{FS = "\t"}{print $2}')
    submitter=$(p4 describe -s "$file" | awk '/^Change/ {split($0, a, " "); print a[3]}; /^User/ {split($0, a, " "); print a[2]}')
    echo "$line $submitter" >> "$csvfile"
done

请注意:

  • 我无法验证第18行ff(submitter= ...),因为我没有可用的P4,并且没有提供样本输出。
  • 我对变量加了双引号,因为它们可能包含白色。即使这可能是意料不到的,这是一个好习惯。

如果输出仍然不符合预期,请提供

p4 describe -s "$file"

$dir1$dir2的典型含量

相关问题