LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 04-26-2011, 06:22 PM   #1
vomplete
LQ Newbie
 
Registered: Nov 2010
Posts: 5

Rep: Reputation: 0
[scripting] try to passing arguments to for cycle (inside a function)


Hi

The code:

Quote:
#!/bin/bash
# Dummy list
ls -1 / >> list.txt

# Function definition
ReadFileLines() {
for i in $(cat $1)
do
$2
done
}

# Run
ReadFileLines "list.txt" "echo $i"

Problem: I need a method to maintain the $i variable.
In fact, actually, this variable get lost when executed.
I think that an escape can preserve this variable and permit its execution inside the function, but I've no idea about.

Any ideas?
 
Old 04-26-2011, 06:29 PM   #2
corp769
Guru
 
Registered: Apr 2005
Posts: 5,807

Rep: Reputation: 995Reputation: 995Reputation: 995Reputation: 995Reputation: 995Reputation: 995Reputation: 995Reputation: 995
Are you trying to run the [ReadFileLines "list.txt" "echo $i" ] only once, or do you want it ran every time the for loop sequences? From what I can see though, you can declare another variable and set it to the value of i, just make sure to declare it outside of the for-loop scope.

Kind regards,

Josh
 
Old 04-26-2011, 06:35 PM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi,

try this
Code:
#!/bin/bash
# Dummy list
ls -1 / > list.txt
# Function definition
ReadFileLines() {
for i in $(cat $1)
do
eval $2
done
}
# Run
ReadFileLines "list.txt" 'echo $i'
While the above example "works" I still wonder why you would want to do this in such a peculiar way. Why don't you simply do
echo $i

inside the for-loop instead of expanding a variable that holds an instruction?
 
Old 04-26-2011, 07:01 PM   #4
vomplete
LQ Newbie
 
Registered: Nov 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by crts View Post
Hi,

try this
Code:
#!/bin/bash
# Dummy list
ls -1 / > list.txt
# Function definition
ReadFileLines() {
for i in $(cat $1)
do
eval $2
done
}
# Run
ReadFileLines "list.txt" 'echo $i'
While the above example "works" I still wonder why you would want to do this in such a peculiar way. Why don't you simply do
echo $i

inside the for-loop instead of expanding a variable that holds an instruction?



thanks 10000!!!

solved!

Last edited by vomplete; 04-26-2011 at 07:10 PM.
 
  


Reply

Tags
bash scripting


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing data from interrupt handler function to tasklet function in kernel programmin double-out Programming 2 05-18-2010 10:10 PM
bash, passing arguments Four Linux - Newbie 3 02-06-2006 08:24 AM
C++, indefinite function arguments and multiple constructor passing @_@ R00ts Programming 2 04-08-2005 03:33 PM
Passing Arguments into the Thread Function George_gk Programming 2 01-31-2005 05:03 AM
Handline passing arguments in C AMMullan Programming 9 03-22-2004 01:37 AM


All times are GMT -5. The time now is 08:09 AM.

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