LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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-22-2012, 08:47 AM   #1
jao_madn
Member
 
Registered: Jun 2010
Posts: 47

Rep: Reputation: 0
script arg or parameters limitation


Hi,

I would like to ask some info on the script arguments/parameters. Does script arguments had limitation like inside or deep inside the loop in which it will not function as it use to be.

as an example:
$2 not working
Code:
command2 () {
for log in $LOG{1,2,3}
	do
	if [ -e $log ] && [ -s $log ]; then
	echo
	echo BEGIN $log
	grep -E "$DATE" $log | grep -E "$2"  2> $SCRIPT
	echo
	echo END $log
	echo
	fi
	done
}
$2 need to declare as another variable to work

Code:
par2=echo $2
command2 () {
for log in $LOG{1,2,3}
	do
	if [ -e $log ] && [ -s $log ]; then
	echo
	echo BEGIN $log
	grep -E "$DATE" $log | grep -E "$par2"  2> $SCRIPT
	echo
	echo END $log
	echo
	fi
	done
}
 
Old 04-22-2012, 09:25 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

How are you calling the command2 function?

Could it be that you're confusing arguments given to the script with those given to a function?

If you run your script like this:
Code:
./script.sh one two
one and two are represented by $1 and $2 in the script, but any function in that script isn't aware of that.

A function also uses $1, $2, etc. These hold the values the function was called with.

If you use command2 $1 $2 it will work.

Have a look at this:
Code:
#!/bin/bash

function foo() {
  echo "Inside function"
  echo $1
  echo $2
  echo $3
  echo " ------------------"
}

echo "Outside function"
echo $1
echo $2
echo $3
echo " ------------------"

foo "Hello" $2 "Bye"
Run this as: ./script.sh one two three

This is one of the reasons why it is good practise to properly name your variables, it makes things more readable and also reduces the chance of mix-ups.

Hope this helps.

Last edited by druuna; 04-22-2012 at 09:41 AM. Reason: grammar
 
1 members found this post helpful.
Old 04-22-2012, 09:36 AM   #3
jao_madn
Member
 
Registered: Jun 2010
Posts: 47

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by druuna View Post
Hi,

How are you calling the command2 function?

Could it be that you confusing arguments given to the script with those given to a function?

If you run your script like this:
Code:
./script.sh one two
one and two are represented by $1 and $2 in the script, but any function in that script isn't aware of that.

A function also uses $1, $2, etc. These hold that values the function was called with.

If you use command2 $1 $2 it will work.

Have a look at this:
Code:
#!/bin/bash

function foo() {
  echo "Inside function"
  echo $1
  echo $2
  echo $3
  echo " ------------------"
}

echo "Outside function"
echo $1
echo $2
echo $3
echo " ------------------"

foo "Hello" $2 "Bye"
Run this as: ./script.sh one two three

This is one of the reasons why it is good practise to properly name your variables, it makes things more readable and also reduces the chance of mix-ups.

Hope this helps.
@druuna: thanks, very much appreciated, you just save me some time digging/reading in the advance shell script guide about functions..
 
Old 04-22-2012, 09:39 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
You're welcome

Just in case:

Bash resources:

BTW: Can you put up the [SOLVED] tag.
first post -> Thread Tools -> Mark this thread as solved
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Arg! Virtual machine, sudo won't work, can't log in as root, visudo won't work Arg! edpatterson Linux - Newbie 2 01-03-2012 05:33 PM
[SOLVED] Passing parameters to script -help flea89 Linux - Newbie 4 03-13-2010 12:47 PM
Passing parameters to PERL script paddyjoy Programming 3 03-07-2006 11:04 AM
arg. checking help in a script kirmet Programming 12 09-12-2005 01:02 AM
safe script parameters Guttorm Programming 1 03-04-2005 12:19 PM


All times are GMT -5. The time now is 07:51 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration