LinuxQuestions.org
Help answer threads with 0 replies.
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 10-26-2007, 11:20 AM   #1
bengoavs
LQ Newbie
 
Registered: Jun 2006
Posts: 20

Rep: Reputation: 0
Running commands from array in a child bash script


Hi guys, kind of a newbie question, so I apologize in advance.
First I tried to export an array from one script to it's child, and I figured it's not supported in bash (would be happy if someone can correct me). So I pass the array cells one by one. It works for all commands except for those with redirection signs (| > etc.), see example and error below. Your help is appreciated!!

main.sh:
CMDS=("ls -l > /tmp/log")
~/child.sh "${CMDS[0]}"

child.sh:
for i in "$@"
do
if [ "$i" ]; then
echo "$i"
$i
fi
done


./main.sh
ls -l > /tmp/log
ls: >: No such file or directory
ls: /tmp/log: No such file or directory
 
Old 10-26-2007, 12:33 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
child.sh takes each of the strings passed on the command line and tries to execute them as a program. However, it does not split them with IFS. You want to "eval $i", like this
Code:
#!/bin/bash
# this is child.sh

for i in "$@"
do
        if [ "$i" != "" ]; then
                echo "$i"
                eval $i
        fi
done
By the way, you can put ode fragments in [code] tags - this will preserve formatting and use a fixed width font to aid readability.

Regarding he exporting of arrays: environment variables are passed between processes as an array of strings only.

Last edited by matthewg42; 12-15-2007 at 06:19 PM.
 
Old 10-26-2007, 02:16 PM   #3
bengoavs
LQ Newbie
 
Registered: Jun 2006
Posts: 20

Original Poster
Rep: Reputation: 0
Thanks a
Code:
LOT
Man !
 
  


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
bash shell script split array robertngo Programming 13 06-19-2011 11:01 PM
Bash Script Array index value Kedelfor Programming 10 04-29-2009 04:37 AM
killing child processes of a bash script results in strange random kills omnio Programming 6 03-12-2007 07:35 AM
Bash script text line into an array toolshed Programming 1 06-13-2005 05:49 PM
MAJOR problem ... bash script array HELP !!!!! michael_util Slackware 1 02-13-2004 06:51 AM

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

All times are GMT -5. The time now is 10:23 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