Register
It is currently Mon Apr 23, 2018 6:46 pm

ls trouble with filenames with spaces...


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Wed Apr 04, 2018 4:06 pm   

Joined: Wed Apr 04, 2018 3:56 pm
Posts: 1
Hi. I'm having trouble when I run this script.

Code:
#!/bin/bash

#lsc, by Thomas Mahoney

if [ $1 == "-h" ] ; then
   echo -e "\nUsage:  lsc <pathtodir>"
   echo -e "Lists directory contents in a single column."
else
   for i in `ls -Q $1` ; do
      echo -e "$i"
   done
fi


It's supposed to list the files in a directory in a single column, but, instead, it gives this output...

Code:
"Bash-Beginners-Guide.pdf"
"Database"
"Qt5"
"Query"
"Scripts"
"The
Cerebrum"
"The
Cerebrum.odt"
"ThomasLMahoneyResume.pdf"
"Transition
to
Qt5.odt"
"What
is
Autism?"
"What
is
Autism?~"


Instead of...

Code:
"Bash-Beginners-Guide.pdf"
"Database"
"Qt5"
"Query"
"Scripts"
"The Cerebrum"
"The Cerebrum.odt"
"ThomasLMahoneyResume.pdf"
"Transition to Qt5.odt"
"What is Autism?"
"What is Autism?~"


I can't use the first output. Does anyone know how to get the second output on my terminal screen? That would be a big help.

Thanks.


Top
 Profile  
 PostPosted: Thu Apr 05, 2018 3:06 am   

Joined: Thu Apr 05, 2018 2:25 am
Posts: 2
TMahoney1979, hello

You can use "-1" with ls to list in single column
Code:
ls -1Q "${1}"

or without ls
Code:
for line in "${1%/}/"*; do
     echo "\"${line##*'/'}\""
done


If you need to use the output of ls in a script turn the colors off
Code:
ls -1Q --color=never "${1}"


Also if you want to use the output of the ls command in the for loop keep an eye on the quotes.
Code:
#!/bin/bash

#lsc, by Thomas Mahoney

if [ $1 == "-h" ] ; then
   echo -e "\nUsage:  lsc <pathtodir>"
   echo -e "Lists directory contents in a single column."
else
  for n in "$(ls -1Q --color=never "${1}")" ; do
      echo "$i"
   done
fi

If you only want to print the output to the terminal for loop isn't needed here. This is more eficient:
Code:
#!/bin/bash

#lsc, by Thomas Mahoney

if [ $1 == "-h" ] ; then
   echo -e "\nUsage:  lsc <pathtodir>"
   echo -e "Lists directory contents in a single column."
else
    ls -1Q --color=never "${1}"
fi


Top
 Profile  
 PostPosted: Thu Apr 05, 2018 9:47 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 574
Do not use "ls" within scripts.
"ls" gives you a formatted output, not a list of filenames.
Hence you'll always have to write obscure code to circumvent unusual filenames.

Each shell got some mechanics called "globs" to deal properly with all kind of filenames.

Code:
for f in /some/path/*.jpg; do
    ((i++))
    # corrected from
    # echo jpg number $1 has name $f
    # to
    echo jpg number $i has name $f
done


Last edited by uhelp on Sun Apr 08, 2018 3:28 pm, edited 1 time in total.

Top
 Profile  
 PostPosted: Sun Apr 08, 2018 11:41 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 642
hi,

Code:
echo "jpg number _${i}_ has name $f"
even
Code:
echo "jpg number $((++i)) has name $f"


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 25 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP