-1

I am trying to use ls and grep to get the directory count, and store the value in a variable. Unfortunately I am getting this error: ./test.sh: line 3: -l: command not found Here is the script:

#!/bin/bash

assetid=ls -l /home/user/Desktop/folder | grep -c "^d"

echo $assetid
DaniFoldi
  • 451
  • 4
  • 14

3 Answers3

2

Use a loop an explicitly count each item resulting from a pattern.

count=0
for f in /home/user/Desktop/folder/*/; do
    count=$((count + 1))
done

If you need to include hidden directories as well, use

count=0
for f in /home/user/Desktop/folder/*/ /home/user/Desktop/folder/.*/; do
    count=$((count + 1))
done

If you aren't concerned about memory usage, fill an array and get the size of the resulting array

dirs=( /home/user/Desktop/folder/*/ )
count=${#dirs[@]}
chepner
  • 497,756
  • 71
  • 530
  • 681
1

Sometimes SO formatting is off, but are you really trying to run the command: assetid=ls -l /home/user/Desktop/folder | grep -c "^d"?

If so, that is attempting to run the command -l with the environment variable assetid set to the string ls. You probably intended to do

assetid=$( ls -l /home/user/Desktop/folder | grep -c "^d" )
William Pursell
  • 204,365
  • 48
  • 270
  • 300
1

I guess this will handle all cases:

find . -maxdepth 1 -type d -exec echo \; | wc -l

For each dir I print an empty newline... then count the newlines. Sadly wc does not work on null terminated strings, but we could remove all except zeros and count them:

find . -maxdepth 1 -type d -print0 | tr -cd '\0' | wc -c

As to your script, you are getting the error, because you need to enclose to comment in $( .. ) if you want to save it's output into a variable. Bash is space aware, I mean a=1 is assigment, and a = 1 is run program named a with arguments = and 1. Bash interprets the line: var=1 bash -c 'echo $var' as first it sets var=1 then runs the program bash with arguments -c and 'echo $var'. Try this:

assetid=$(ls -l /home/user/Desktop/folder | grep -c "^d")

But don't parse ls output. ls is for human readable nice colored output, it's better to prefer using different utilities in batch/piped scripts.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111