I have a bash script I am trying to run through directories to the end of the line, for example if I have 4 directories in a row it will go down /d1/d2/d3/d4 until it finds files or other directories. If d3 has a file, it will stop there. I was doing this by counting the lines ls prints to a file and if there's 1 directory use that name in a cd command. My file is:
`
#!/bin/bash
COUNTFILE="/home/username/fake.txt"
ITEMCOUNT=$(ls | wc -l)
echo "ITEMCOUNT" $ITEMCOUNT
echo $*
ONE="1"
echo "one" $ONE
if [["$ITEMCOUNT" -eq "$ONE"]];
then
DIRCOUNT=$(find . -maxdepth 1 -type d | wc -l)
echo "dircount" $DIRCOUNT
else
DIRCOUNT="0"
fi
if [$DIRCOUNT == "1"]; then
ls > $COUNTFILE
PATH=$(head -1 $COUNTFILE)
cd ./$PATH
fi
`
As is I get
pipetester.sh: line 15: [[1: command not found
pipetester.sh: line 2
4: [0: command not found
I checked syntax for 2 hours, but it seems to be complaining about my "if" lines, why?
if (( DIRCOUNT == 1 ))-- may be more readable. Also, using all-caps variable names is bad form for variables internal to your script; by convention, all-caps names are reserved for environment variables and shell builtins to avoid namespace conflicts.@and theCand can then choose from a list of candidates (or type the name manually).