LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Using global variable in awk (bash script) (https://www.linuxquestions.org/questions/programming-9/using-global-variable-in-awk-bash-script-548386/)

kopeda 04-23-2007 04:25 PM

Using global variable in awk (bash script)
 
Hello!
I'm making a script, that counts the file types listed in the arguments in a folder, like this: ./dirstat png,txt,sh -d /Home
But there is a problem with it, I think in the line witch contains awk, because I'm trying to use a bash variable. I've signed the line, with big bold font. Pls help me! Thank you!!
this is the code (sorry, but the comment are hungarian) :

#!/bin/bash


function errormsg(){
if [ $1 -eq 1 ]; then
echo "A program megszámolja a fájltĂ*pusok "
echo "Használat:"
echo "dirstat.sh kiterjesztés1,kiterjesztés2,... -d "

elif [ $1 -eq 2 ]; then
echo "Hiba: Nem Ă*rhatĂł a logfájl könyvtára."

elif [ $1 -eq 3 ]; then
echo "Hiba: A vizsgálandó könyvtár jogosultságai nem"
fi
exit $1;
}


DIRECTORY="."
HAVETOLOG=1;


shift
while [ $# -gt 0 ]; do
if [ $1 = "-l" ]; then
HAVETOLOG=0;
shift
LOGFILENAME=$1

if [ -z $LOGFILENAME ] || [ `echo -n $LOGFILENAME |cut -c 1
>/dev/null` = '-' ]; then
errormsg 1;

elif [ ! -w `dirname $LOGFILENAME 2>/dev/null` ]; then
errormsg 2;
fi
elif [ $1 = "-d" ]; then
shift
DIRECTORY=$1

elif [ $1 = "-?" ] ; then
errormsg 1

fi
shift
done




TIP=1
I=0

for (( I=0; TIP > 0; I=I + 1));
do

#I think here is the problem:
TIP= `echo $1|tr "," " "|awk ' { printf $"$I" }' `
FILENUMBER=`ls -lR "$DIRECTORY" | grep "^-" |grep "\."$TIP"$"|wc -l `






OUTPUT="${DIRECTORY}/ könyvtárban ${FILENUMBER} darab ${TIP} "

echo "$OUTPUT"


if [ $HAVETOLOG -eq 0 ]; then
echo "$OUTPUT" > $LOGFILENAME
fi

done

exit 0

acid_kewpie 04-23-2007 04:45 PM

from the awk manpage:
Quote:

-v var=val
--assign var=val
Assign the value val to the variable var, before execution of the program begins. Such variable values are available to the
BEGIN block of an AWK program.
so "-v p=$PATH" would set the "p" variable in the awk script to the bash $PATH variable.

kopeda 04-24-2007 01:47 AM

Thank you, but I don't understand you reply, because I'm neither good in English, nor in BAsh, so if you or somebody else has time to give me an exact solution, I would apreciate it. Thank you!:newbie:


All times are GMT -5. The time now is 11:03 PM.