3

This script is supposed to print out the file size and the name of all files in a directory that are under 100 bytes. This script I wrote works perfectly on my Ubuntu, but won't work on my Mac OS Lion. Can anyone tell me why?

#!/bin/bash

for i in $(ls)
do
  if [[ $(wc -c $i | cut -d" " -f1) -le 100 ]]; then
    echo $(wc -c $i)
  fi
done
  • 2
    Useless Use of Ls in Backticks. You want for i in * instead. partmaps.org/era/unix/award.html#ls – tripleee Jan 31 '13 at 8:11
  • Do you men less than 100 bytes, or 100 bytes or less? Your question is not consistent on that point. – tripleee Jan 31 '13 at 8:39
4

Wow. It looks like you're making this more difficult than it has to be:

find . -size -100c -exec stat -c "%s : %n" {} \;

Done.

  • 1
    note that OSX's default "stat" program has different parameters. You could just as easily call ls instead of stat, though. If you're sane, you install GNU coreutils immediately when you get your mac, though. – tylerl Jan 31 '13 at 2:44
0

See this post: Nested bash if/and/or statement not working going from OS X to Ubuntu

Ubuntu uses dash, I'm not sure if you're using bash or what on OSX. Put this at the top to guarantee that you're using the same shell on both systems:

#! /usr/bin/env bash

-or-

#!/bin/bash

Also you can echo $SHELL to see what shell you're using from the command line. With bash scripts you can turn on debugging by adding this line at the top:

set -x

For example

% ls -l
total 4
-rw-rw-r-- 1 saml saml   0 Jan 30 20:30 a1
-rw-rw-r-- 1 saml saml   0 Jan 30 20:30 a2
-rwxrwxr-x 1 saml saml 151 Jan 30 20:45 a.bash

% ./a.bash 
0 a1
0 a2

with set -x enabled

% ./a.bash 
++ ls
+ for i in '$(ls)'
++ wc -c a1
++ cut '-d ' -f1
+ [[ 0 -le 100 ]]
++ wc -c a1
+ echo 0 a1
0 a1
+ for i in '$(ls)'
++ wc -c a2
++ cut '-d ' -f1
+ [[ 0 -le 100 ]]
++ wc -c a2
+ echo 0 a2
0 a2
+ for i in '$(ls)'
++ wc -c a.bash
++ cut '-d ' -f1
+ [[ 123 -le 100 ]]

Resources

  • sorry i forgot to mention they are both bash scripts and yes i have #!/bin/bash. Just the script works on ubuntu but not mac bash – Shawn Jan 31 '13 at 1:56
  • What does your script show when set -x is enabled on both? Also I would be highly suspect of versions. A bash --version will show that. – slm Jan 31 '13 at 1:57
0

Refactored to use a single external process:

for i in *; do
  wc=$(wc -c <"$i")
  case $wc in 100 ) ;; ???* ) continue ;; esac
  echo "$wc $i"
done

Notice the quoting, the use of redirection to avoid the need for cut, and the use of a wildcard pattern to exploit the happy fact that any number with three or more digits is bigger than or equal to 100.

(Your code says less than or equal to 100 while the problem description says genuinely less than 100; you can take out the 100) ;; case if your code is wrong and your stated question is correct. )

Note that wc -c counts characters, not bytes; for text in a variable-length or multibyte encoding such as Unicode, they are not the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.