Shell: Sorting file whose name contains white space

0

Using shell script, I am creating a script.sh file that automates some tasks for me. In it I need to sort a file from the current directory to perform some tasks. I am raffling the file with the command below:

shuf -n1 -e $(ls)

The code works perfectly when the file name does not contain white space (ex: relatorio.pdf ). However, for files with whitespace (eg relatorio mensal.pdf ), the draw ends up breaking the filename (in my example, it is relatorio and mensal.pdf ).

I would like the shuf command to draw and always return the full name of the file, even if it contains white space. Does anyone know how to do this?

    
asked by anonymous 17.04.2017 / 22:20

1 answer

0

The parameter -e was used incorrectly. It indicates that each passed argument will be treated as an input. This way, relatorio is an argument and mensal.pdf is another.

The command that solves the problem is

ls | shuf -n 1

In this way, the first statement is executed: ls (lists the contents of the directory). The result is passed to the statement shuf , which will randomly draw a line from the ones returned by the ls command. This is said with the parameter -n 1 .

    
18.04.2017 / 03:05