LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-23-2007, 04:25 PM   #1
kopeda
LQ Newbie
 
Registered: Apr 2007
Posts: 2

Rep: Reputation: 0
Question 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
 
Old 04-23-2007, 04:45 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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.
 
Old 04-24-2007, 01:47 AM   #3
kopeda
LQ Newbie
 
Registered: Apr 2007
Posts: 2

Original Poster
Rep: Reputation: 0
Unhappy

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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
can awk see bash script arguments ? sharapchi Programming 7 12-14-2006 08:03 PM
Bash Awk Variable problem _hadi_ Programming 5 12-13-2006 12:25 AM
cannot export result from awk into a variable in a bash script Emmanuel_uk Linux - Newbie 4 03-07-2005 01:54 AM
Accessing Shell variable in awk script dileepkk Linux - General 1 10-07-2004 07:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration