Shell: Draw file whose name contains blank space

Asked

Viewed 53 times

0

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

shuf -n1 -e $(ls)

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

I would like the command shuf draw and always return the full name of the file, even if it contains blank space. Someone knows how to do this?

1 answer

0


The parameter -e was incorrectly used. It indicates that each argument passed 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 instruction is executed: ls (lists the contents of the directory). The result is passed to the statement shuf, that will draw a random line among those returned by the command ls. This is said with the parameter -n 1.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.