Required Bash expert; channel wealth through Cowsay using a whimsical cow.
Required Bash expert; channel wealth through Cowsay using a whimsical cow.
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.
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
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.