LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   shell script programming problem (https://www.linuxquestions.org/questions/programming-9/shell-script-programming-problem-217962/)

newbie_ken 08-15-2004 02:31 PM

shell script programming problem
 
hi guys,

i'm new to linux and i am facing a problem with filenames that have spaces in them.

find $folder -type f -printf "%f/n" > file.tmp

I use the above line to copy filenames in a folder to a temp file, but if the filename contain spaces it will be written as 2 separate lines.

example: the filename "kenny rogers" will be displayed as

kenny
rogers

what should i do to?

thanks
Ken

Dark_Helmet 08-15-2004 02:55 PM

Your command works as expected, but your script doesn't. We'll need to see the script to give an accurate solution.

newbie_ken 08-15-2004 10:23 PM

Im trying to find files in a directory and sort them out according to highest frequency, then alphabetical order.

My code:

#!/bin/sh

start_folder=""

if [ "$1"="" ]; then
start_folder=.
else
start_folder=$1
fi

find $start_folder -type f -printf "%f\n" > file1.tmp
cat file1.tmp|uniq > file2.tmp

for line in `cat file2.tmp` # problem starts here as the for loop treats the space as a delimiter
do
count=`grep -x $line file1.tmp|wc -l`
echo $line $count >> file3.tmp
done

sort -k 2nr -k 1 file3.tmp | cut -d " " -f1 | head

rm *.tmp

Dark_Helmet 08-15-2004 11:04 PM

Ok, I was worried you might be doing something non-standard. This is my normal solution to this kind of problem:

Code:

#!/bin/sh

start_folder=""

if [ "$1"="" ] ; then
  start_folder=.
else
  start_folder=$1
fi

find $start_folder -type f -printf "%f\n" > file1.tmp
cat file1.tmp|uniq > file2.tmp

old_ifs=$IFS
IFS=$'\n'

for line in `cat file2.tmp` # problem starts here as the for loop treats the space as a delimiter
do
  count=`grep -x "$line" file1.tmp|wc -l`
  echo $line $count >> file3.tmp
done
IFS=$old_ifs

sort -k 2nr -k 1 file3.tmp | cut -d " " -f1 | head

rm *.tmp

As a very quick explanation, the IFS variable defines what characters act as delimiters. So the lines in red simply store the normal value, replace that list with the newline as the only delimiter, do your processing, and restore the old value.

Also, the reason you need to surround $line in double quotes is for a similar reason. If your files contain spaces, grep can't know when your expression ends and filename(s) begin.

newbie_ken 08-16-2004 03:57 AM

Thanks a lot!!! Dark.
You're a life saver.
By the way what commands can i use to test for empty files or directories?
I'm still new to linux and not used to the syntax.

Dark_Helmet 08-16-2004 11:19 AM

The normal method is to use something like this:
Code:

if [ -e $some_filename ] ; then
  file/directory exists
else
  file/directory not found
fi

The [ ] is a shorthand for the test command. You can snoop around what tests are available by reading the man pages: man test


All times are GMT -5. The time now is 06:48 PM.