Bruno Soares Bruno Soares - 10 months ago 110
Bash Question

Getting path from find command to variable

So this is my shell script so far, it's very simple
I just want the user to give a directory, then it'll find it and give back the permissions and name of the files in the directory.

echo '#!/bin/bash' > ex1
echo 'echo 'Which is the directory to list?'' >> ex1
echo 'read directory' >> ex1
echo 'path=`find / -type d -name $directory`' >> ex1
echo '$(find $path -printf '%M %P\n')' >> ex1


I've done that so far but when I run it, it gets me an error:

./ex1: line 5: drwxr-xr-x: command not recognized


(ex1 is the name of the shell script and it's inside the folder I'm using to test the shell script)

Answer Source
echo '$(find $path -printf '%M %P\n')' >> ex1

Get rid of the $(...). Just run the find command directly. You also should fix the inner single quotes, such as by switching them to double quotes.

echo 'find $path -printf "%M %P\n"' >> ex1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download