LinuxQuestions.org
Help answer threads with 0 replies.
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, 12:20 PM   #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, 01:33 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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 07:19 PM.
 
Old 10-26-2007, 03:16 PM   #3
bengoavs
LQ Newbie
 
Registered: Jun 2006
Posts: 20

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


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


All times are GMT -5. The time now is 10:11 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration