77.0k views
1 vote
Write a BASH script called yoda.sh that takes as a command line argument the pathname of a text file. The script will then read the lines of text, randomly permute the words within each line, and give the permuted line on stdout; i.e. for each line, one of the many possible permutations of the words is randomly selected. For example, the input file sample.txt

contains the following:

this is a line containing many words
and this is also a line but containing more words
not many words
42 is the answer

User Wm
by
7.8k points

1 Answer

1 vote

Final answer:

To create the yoda.sh script, you can use the bash scripting language. The script reads the lines of text from a given text file and randomly permutes the words within each line. Here is an example implementation of the yoda.sh script:

Step-by-step explanation:

To create the yoda.sh script, you can use the bash scripting language. The script takes a text file pathname as a command line argument and reads the lines of text. To randomly permute the words within each line, you can use the shuf command in combination with the tr command to split the line into words and then shuffle them. Finally, you can use the printf command to output the permuted line.

Here is an example implementation of the yoda.sh script:

#!/bin/bash

destination_file=$1

while IFS= read -r line

do
permuted_line=$(echo "$line" | tr ' ' '
' | shuf | tr '
' ' ')
printf "%s\\" "$permuted_line"
done < "$destination_file"

User Steven McGrath
by
8.0k points