I am creating a bash script that will set a condition to execute a certain command if the line count exceeded per file.
These are the name of the files
dir=/tmp
$dir/filename_$date_00.csv
$dir/filename_$date_01.csv
$dir/filename_$date_02.csv
$dir/filename_$date_03.csv
$dir/filename_$date_04.csv
$dir/filename_$date_05.csv
$dir/filename_$date_06.csv
I want to be able to create a loop with a concept of something like this (i know that the syntax is incorrect but kindly just consider the concept)
if ["cat $dir/filename_$date_00.csv | wc -l" -gt 10]
then
echo "sorry, cannot proceed"
else,
cat $dir/filename_$date_00.csv >> log..txt
fi
then loop until it reaches $dir/filename_$date_03.csv and ignore $dir/filename_$date_04.csv onwards
I want to be able to create a loop to simplify code instead of printing/repeating everything and have a long code. Please help. thank you!
filename_$date_03.csv? Do you always only process the first 4 files, or does this vary?head -n 11 file | wc -land still check whether it got-gt 10: any further lines are irrelevant.date, you have to put curly braces around it${date}. Otherwise, the underscore and the number are part of the variable name.for i in 0 1 2 3; do # process $dir/filename_${date}_0$i.csv; donewill do what you want.