Reputation: 5420
I never used shell script, but now I have to , here is what I'm trying to do :
#!/bin/bash
echo running the program
./first
var = ($(ls FODLDER |wc -l)) #check how many files the folder contains
echo $var
if( ["$var" -gt "2"] #check if there are more the 2file
then ./second
fi
the scriopt crashes at the if
statement. how may I solve this
Upvotes: 0
Views: 61
Reputation: 43391
Edit your script.bash file as follow:
#!/bin/env bash
dir="$1"
echo "running the program"
./first
dir_list=( $dir/* ) # list files in directory
echo ${#dir_list[@]} # count files in array
if (( ${#dir_list[@]} > 2 )); then # test how many files
./second
fi
script.bash /tmp/
You need to learn bash to avoid dangerous actions!
/tmp/
→ `$1) dir_list
) containing all file in given directory${#dir_list[@]}
)Upvotes: 2
Reputation: 289495
Many:
var = ($(ls FODLDER |wc -l))
This is wrong, you cannot have those spaces around =
.
if( ["$var" -gt "2"]
Your (
is not doing anything there, so it has to be deleted. Also, you need spaces around [
and ]
.
All together, this would make more sense:
#!/bin/bash
echo "running the program"
./first
var=$(find FOLDER -maxdepth 1 -type f|wc -l) # better find than ls
echo "$var"
if [ "$var" -gt "2" ]; then
./second
fi
Note:
echo
, specially when handling variables.Upvotes: 5