Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
763 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...