I'm trying to write a little script to list a directory from a given variable.
However, I can't run ls at all after reading my input into the variable PATH.
#!/system/bin/sh
echo "enter directory for listing"
read "PATH"
ls "$PATH" -R > list.txt
This exits with:
ls: not found
...and writes nothing to list.txt.
The variable name PATH is already reserved for a different purpose: It lists all the possible locations searched to find commands not built into the shell.
ls is such a command. Thus, when you change the value of PATH, you change the way the shell tries to look for the ls executable; unless the new value of PATH includes a directory with a ls executable in it, any further attempts to run ls (or other commands not built into the shell) will fail.
Instead, use a different variable name -- ideally, including at least one lower-case character, to avoid conflict with (all-uppercase) builtins and environment variables.
Thus, one corrected form might be:
#!/system/bin/sh
echo "enter directory for listing"
IFS= read -r path
ls -R -- "$path" > list.txt
Note that the -R is moved before the "$path" in this case -- while GNU systems will allow optional arguments to be after positional arguments, many older UNIX systems will only treat flags (like -R) as valid if they're found before the first non-flag/option argument.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With