简体   繁体   English

将打印头集成到单行 bash CLI

[英]integrate head in a one-liner bash CLI

I have this command我有这个命令

$ sha1sum file.bin | cut -f1 -d " "

that computes the sha1sum of some eeprom content.计算一些eeprom内容的sha1sum。 The pipe followed by cut is to get rid of the file location in the output and retrieve only the sha1. pipe 后跟 cut 是为了摆脱 output 中的文件位置并仅检索 sha1。

My issue is that I need to compute the sha1 over the first N lines of the file.bin .我的问题是我需要在file.bin的前N行计算 sha1。 The file is a table/array of lines of 32 bytes (fixed lengths).该文件是一个 32 字节(固定长度)行的表/数组。

I know I have to put in some head -n N , but don't know how with the redirections.我知道我必须放入一些head -n N ,但不知道如何使用重定向。 The syntax is head -n N <file> .语法是head -n N <file>

I tried naively sha1sum file.bin | cut -f1 -d " " | head -n 8我天真地尝试过sha1sum file.bin | cut -f1 -d " " | head -n 8 sha1sum file.bin | cut -f1 -d " " | head -n 8 sha1sum file.bin | cut -f1 -d " " | head -n 8 but it doesn't work. sha1sum file.bin | cut -f1 -d " " | head -n 8但它不起作用。 I need the shell to understand somehow this: sha1sum(head -n 8 file.bin) | cut -f1 -d " "我需要 shell 以某种方式理解这一点: sha1sum(head -n 8 file.bin) | cut -f1 -d " " sha1sum(head -n 8 file.bin) | cut -f1 -d " " , but we can't use mathematical function syntax in shell I guess... sha1sum(head -n 8 file.bin) | cut -f1 -d " " ,但我猜我们不能在 shell 中使用数学 function 语法...

How to do it please?请问怎么做? Regards问候

Make the output of head the input of sha1sum , just as the output of sha1sum is the input to cut .head的 output 作为sha1sum的输入,就像sha1sum的 output 作为cut的输入一样。

head -n 8 file.bin | sha1sum | cut -d " " -f1

or, if the file is binary, use the first N bytes with -c或者,如果文件是二进制文件,则将前 N 个字节与-c一起使用

head -c 1024 file.bin | sha1sum | cut -d " " -f1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM