pipe command for array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting pipe command for array
# 1  
Old 06-05-2009
pipe command for array

Hi Folks,

very basic question, how to do command piping for an array?

suppose i have names as an array, when I write this script:

#!/usr/bin/sh
date=`date +%y%m%d`;
names="a b"
for name in ${names[@]}
do extract -tz +8 person 'income$|expense$' /home/ricki/$name/$date*.xml | tab -d -cols ctr -fmt csv | tee /home/ricki/$name_$date.txt
done

extract and tab are existing perl scripts.

seems that the names array is not stored after executing the first command.. sorry if the question is very basic, i'm a newbie in shell.

//ricki

Last edited by rickirick; 06-05-2009 at 03:10 PM..
# 2  
Old 06-05-2009
names is not and never was an array. The shell splits it into a list when you give it ${names}, the same way echo is given the arguments a and b seperately when you do echo a b . The shell always does this unless you force it not to by surrounding a string with double quotes, i.e. "${names}". Single quotes, on the other hand, force it to take the string entirely literally and substitute no variables at all.

/bin/sh in fact is not strictly required to support arrays at all, but bash or ksh both do and often stand in for plain sh anyway. If your program demands arrays it ought to use one of those and not sh.

An array and loop in bash:

Code:
#!/bin/bash
NAMES=( a b )
for ((N=0; N<${#NAMES[*]}; N++))
do
  echo "${NAMES[$N]}"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Single command pipe

Single command to ls all the files inside a particular directory hierachy and output this to a file and open this in a vim file so that i can use gf command in vim to browse through all the files inside this hierachy. eg : dir1/dir2 and dir1/dir3 dir2 and dir3 contain the files i need... (7 Replies)
Discussion started by: dll_fpga
7 Replies

2. Shell Programming and Scripting

pipe in command

Hello, I try to concatenate a command to execute. Sadly it throws an error. #!/bin/bash cd / cmd="find -name *.txt | awk '{ printf "FILE: "$1; system("less "$1);}' | egrep 'FILE:|$1'" echo "1." $($cmd) echo "2." $("$cmd") echo "3." `$cmd` echo "4." `"$cmd"`1.&3. 'find: paths must... (2 Replies)
Discussion started by: daWonderer
2 Replies

3. UNIX for Dummies Questions & Answers

using 'more' instead of 'cat' to pipe into another command

Hi, quick question. Since i am in the habit of using 'more' to view files, e.g. $ more my_file.txt I often use this command to pipe into other utilities like grep. For example, $ more my_file.txt | grep '^>' would get all lines in the file that begin with ">". I know that it is... (6 Replies)
Discussion started by: css136
6 Replies

4. UNIX and Linux Applications

Tee with pipe command.

cat work.txt M|324324|32424|3431 M|324324|32424|3431 N|324324|32426|3432 N|324324|32424|3434 M|324324|32424|3435 cat work.txt | tee $( grep '^M' > m.txt ) | $( grep '^N' > n.txt ) cehpny00:/home01/sr38632 $ cat m.txt M|324324|32424|3431 M|324324|32424|3431 M|324324|32424|3435 ... (2 Replies)
Discussion started by: rsampathy
2 Replies

5. Shell Programming and Scripting

pipe grep command

Hi all, Can someone help me with the following problem. I am executing the following command: (search for occurences of 'error' in files that match cl-*.log expression) > grep -cw -i --max-count=1 'error' cl-*.log this command outputs: cl-apache.log:1 cl-apache_error.log:1... (3 Replies)
Discussion started by: epro66
3 Replies

6. Shell Programming and Scripting

Pipe text from a file into an array

Hi Guys I have a question about filling up an array I have a file called USER_FILE.txt it contains the following: Real Name:Thomas A Username:THOMAS_A Real Name:Thomas B Username:THOMAS_B Real Name:Thomas C Username:THOMAS_C Real Name:Thomas D Username:THOMAS_D Real Name:Thomas E... (8 Replies)
Discussion started by: grahambo2005
8 Replies

7. UNIX for Dummies Questions & Answers

Pipe in command string

Hi, Can't you have a pipe in a command string ? If I try the following I get errors. Why ? > cmd="ls -lrt | grep xyz" > $cmd |: No such file or directory grep: No such file or directory xyx: No such file or directory Thanks in advance Hench (3 Replies)
Discussion started by: hench
3 Replies

8. UNIX for Dummies Questions & Answers

How can I use pipe command ?

Hi My friends I have used this command to find files are modified within the past 24 hours and then many files are shown but I want transfer all these files to special directory by using pipe . can any one tell me what is the next step ? (11 Replies)
Discussion started by: bintaleb
11 Replies

9. UNIX for Dummies Questions & Answers

pipe command

current dir : /home/sales ls -l abc.txt 17th aug bcd .txt 16t oct ------- ------ Total files : 100 if i want to move only those files dated 17 aug into another sub directory /home/sales/texas how do i pipe the result of 'ls' command to a 'mv' command (1 Reply)
Discussion started by: zomboo
1 Replies

10. UNIX for Advanced & Expert Users

How to pipe command

Hi All, I want to create a command that executes a text editor with the most recent file in the current current directory. So a good start to achieve this is : ls -lrt | cut -c55- | tail -1 which provides the name of the most recent file in a directory The problem is to pipe the... (4 Replies)
Discussion started by: anonymous.nico
4 Replies
Login or Register to Ask a Question