Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
768 views
in Technique[技术] by (71.8m points)

awk in bash with ls and variable

I wanted to print only the name of files of a specific directory: In this way it works:

ls -g  --sort=size -r /bin | awk '{print $8,$9,$10,$11,$12,$13}'

but if I read the path variable it doesn't work:

read PATH
ls -g  --sort=size -r $(PATH) | awk '{print $8,$9,$10,$11,$12,$13}'
Command 'awk' is available in '/usr/bin/awk'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is exactly why you should not use UPPER_CASE_VARS. $PATH is a variable used by the shell to find executables on your system. As soon as you over-write it with user input, your script can no longer find anything that does not reside in whatever the input was. In this case, you entered /bin, so your script can find /bin/ls but awk is not there.

The command_not_found_handle (see /etc/bash.bashrc) stepped in to give you a suggestion.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...