F5F Stay Refreshed Software Operating Systems Required Bash expert; channel wealth through Cowsay using a whimsical cow.

Required Bash expert; channel wealth through Cowsay using a whimsical cow.

Required Bash expert; channel wealth through Cowsay using a whimsical cow.

M
MinaMoo
Member
210
11-28-2024, 01:03 AM
#1
I've modified a script I built a couple of months ago to add to my .bash_profile. It randomly selects a file from the specified directory, appends a string, and feeds it to cowsay. I'm aiming for about 90% accuracy, but I need a bit more precision. The current command looks like this:

command ls /usr/local/share/cows/ | sort -R | head -1 | (printf " -f /usr/local/share/cows/" && cat) | (cat && echo $(fortune -a)) | xargs cowsay

The issue is that when the fortune output contains quotes, it breaks the command flow. Switching to xargs with -0 helps by passing the whole string, but it still doesn't resolve the problem. I've experimented with custom delimiters and separate arguments, but nothing seems to fix it. Please let me know if you can assist further.
M
MinaMoo
11-28-2024, 01:03 AM #1

I've modified a script I built a couple of months ago to add to my .bash_profile. It randomly selects a file from the specified directory, appends a string, and feeds it to cowsay. I'm aiming for about 90% accuracy, but I need a bit more precision. The current command looks like this:

command ls /usr/local/share/cows/ | sort -R | head -1 | (printf " -f /usr/local/share/cows/" && cat) | (cat && echo $(fortune -a)) | xargs cowsay

The issue is that when the fortune output contains quotes, it breaks the command flow. Switching to xargs with -0 helps by passing the whole string, but it still doesn't resolve the problem. I've experimented with custom delimiters and separate arguments, but nothing seems to fix it. Please let me know if you can assist further.

K
kbolt
Member
238
11-28-2024, 01:03 AM
#2
Review the content carefully and let me know if you need any adjustments.
K
kbolt
11-28-2024, 01:03 AM #2

Review the content carefully and let me know if you need any adjustments.

K
kroko_53
Junior Member
34
11-28-2024, 01:03 AM
#3
By passing cow file contents via standard input, you’re not cleaning the input, which is risky because I could insert malicious code. Have you considered storing the sorted output in a variable? COWFILE=$(command ls /usr/local/share/cows/ | sort -R | head -1); Use $COWFILE to display cowsay. I’m not using Linux, so if this doesn’t work you might need to echo the file name. But I think it should be fine after reading correctly. I believe you’ve gone too far with the pipes; maybe try listing and sorting once, then use cowsay. fortune | cowsay -f `ls -1 /usr/local/share/cows/ | sort -R | head -1` -n or COWFILE=$(command ls /usr/local/share/cows/ | sort -R | head -1); fortune | cowsay -f $COWFILE -n
K
kroko_53
11-28-2024, 01:03 AM #3

By passing cow file contents via standard input, you’re not cleaning the input, which is risky because I could insert malicious code. Have you considered storing the sorted output in a variable? COWFILE=$(command ls /usr/local/share/cows/ | sort -R | head -1); Use $COWFILE to display cowsay. I’m not using Linux, so if this doesn’t work you might need to echo the file name. But I think it should be fine after reading correctly. I believe you’ve gone too far with the pipes; maybe try listing and sorting once, then use cowsay. fortune | cowsay -f `ls -1 /usr/local/share/cows/ | sort -R | head -1` -n or COWFILE=$(command ls /usr/local/share/cows/ | sort -R | head -1); fortune | cowsay -f $COWFILE -n

F
Firewolf361
Junior Member
49
11-28-2024, 01:03 AM
#4
This one functions perfectly, I just had to insert a "command" before ls since my alias points to ls -lhaG. The formatting of text within the cowsay can be inconsistent, but this version is significantly more concise and performs better than before.
F
Firewolf361
11-28-2024, 01:03 AM #4

This one functions perfectly, I just had to insert a "command" before ls since my alias points to ls -lhaG. The formatting of text within the cowsay can be inconsistent, but this version is significantly more concise and performs better than before.