-1

i am a new user and I was trying to build a script with which i can count how many files are in a direcotry, i did this but it does not seem to work.

#! /bin/bash
read -p 'path: ' dir

dir=$dir

if [ "$dir" != "stop" ]
then
    exit
fi

while [ -d $dir ]
do
    wc=$(( ls $dir | wc -l ))
done

echo $wc
markp-fuso
  • 28,790
  • 4
  • 16
  • 36
  • are you sure you want to `exit` if the user inputs anything **other than** `stop`? do you want `[ "$dir" = "stop" ]` ? – markp-fuso Dec 11 '20 at 16:23
  • I wanted to exit the script if stop was imputed. Thanks for the adjustment. i modified the code and now i've got this error: line 13: ls /home | wc -l : division by 0 (error token is "home | wc -l ") – Bioscotch Dec 11 '20 at 16:32

1 Answers1

0

The easier way to get the count will be:

wc=$(find "$dir" -maxdepth 1 - type f | wc -l)
Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18