LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-09-2020, 04:40 PM   #1
DoorDaesh
LQ Newbie
 
Registered: Jul 2020
Posts: 23

Rep: Reputation: Disabled
Why does this script (with pipefail) exit with nonzero status and how can I fix it?


I enabled pipefail in my script for improved safety, but now there is one line that causes the script to fail, even though it's working as intended.

It's this line setting the value of rand_string:

Code:
$ set -o pipefail
$ rand_string=$(cat /dev/urandom | tr -cd 'a-f0-9' | head -c 12)
$ echo $?
# 141
I assume it's either the cat or tr command that's exiting with code 141. Which one is it? Why is it doing that? How can I make it stop?
 
Old 12-09-2020, 05:02 PM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
141 means 141-128=13=SIGPIPE Broken pipe: write to pipe with no readers.

This is how I would do it:
Code:
od -An -N6 -x /dev/urandom | tr -d ' '
Or
Code:
od -An -N6 -tx8 /dev/urandom | tail -c13
Or
Code:
hexdump -n6 -e'"%0x"' /dev/urandom
Or, if you have Vim installed:
Code:
xxd -l6 -p /dev/urandom

Last edited by shruggy; 12-10-2020 at 03:21 AM.
 
3 members found this post helpful.
Old 12-09-2020, 05:26 PM   #3
DoorDaesh
LQ Newbie
 
Registered: Jul 2020
Posts: 23

Original Poster
Rep: Reputation: Disabled
Does SIGPIPE occur because cat is still "printing" even after head takes what it needs?
 
1 members found this post helpful.
Old 12-09-2020, 05:33 PM   #4
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Yes, see this question.
 
Old 12-09-2020, 07:30 PM   #5
DoorDaesh
LQ Newbie
 
Registered: Jul 2020
Posts: 23

Original Poster
Rep: Reputation: Disabled
Thanks for the solution. The od command is new to me, so I had to read up on it. I think I understand the purpose of most of those options now.
  • -An #Suppresses "line numbering"
  • -N6 #Amount to output
  • -tx8 #Controls output format and length of the "words" (8-byte hex)

Could you explain the purpose of -v? The manual says:
Code:
-v, --output-duplicates
       do not use * to mark line suppression
Does this mean that by default it will print an asterisk if a line is identical to the previous one?
 
Old 12-10-2020, 03:33 AM   #6
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Yep. I amended my post and removed -v: it's not required here, as there's only one line of output anyway.

Also, see detailed explanation of pipefail behavior in BashPitfall #60.
 
  


Reply

Tags
bash, pipe, random



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
dhclient-exit-hooks.d/rfc3442-classless-routes returned non-zero exit status Shr0912 Debian 2 01-17-2018 02:40 PM
exit status for system call (exit) Tala_22 Linux - Kernel 2 10-20-2015 06:51 PM
> Ftp With Exit Status, Ftp with exit status vwvr9 Linux - General 4 02-23-2005 02:53 AM
nonzero status in irq -110 jonathon Linux - General 7 08-04-2001 05:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration