Unable to print python array in shell script loop.


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unable to print python array in shell script loop.
# 8  
My original sed solution had a typo. This version works for both the samples you gave, with and without the quotes:
Code:
#!/bin/sh

printf "Parameter 1: %s\n" "$1"
MYARRAY=`echo "$1" | sed 's/^\[//; s/,/ /g; s/\]$//'`

for a in $MYARRAY
do
   a=`expr "$a" : 'u\(.*\)$'`
   printf "Printing Array: %s\n" "${a}"
done

While this is written in dash on Ubuntu, it should work for a 1980s-era Bourne Shell (I've no idea what AIX uses). One caveat is that if any of the strings don't start with a u the value will be lost. If your shell supports the prefix removal parameter expansion you could replace
Code:
   a=`expr "$a" : 'u\(.*\)$'`
   printf "Printing Array: %s\n" "${a}"

with
Code:
printf "Printing Array: %s\n" "${a#u}"

Your latter example should work with this script, using python:
Code:
#!/bin/sh

printf "Parameter 1: %s\n" "$1"
MYARRAY=`echo "print ' '.join($1)" | python`

for a in $MYARRAY
do
   printf "Printing Array: %s\n" "${a}"
done

Both of these have been tested.

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 9  
Quote:
Originally Posted by apmcd47
My original sed solution had a typo. This version works for both the samples you gave, with and without the quotes:
Code:
#!/bin/sh

printf "Parameter 1: %s\n" "$1"
MYARRAY=`echo "$1" | sed 's/^\[//; s/,/ /g; s/\]$//'`

for a in $MYARRAY
do
   a=`expr "$a" : 'u\(.*\)$'`
   printf "Printing Array: %s\n" "${a}"
done

While this is written in dash on Ubuntu, it should work for a 1980s-era Bourne Shell (I've no idea what AIX uses). One caveat is that if any of the strings don't start with a u the value will be lost. If your shell supports the prefix removal parameter expansion you could replace
Code:
   a=`expr "$a" : 'u\(.*\)$'`
   printf "Printing Array: %s\n" "${a}"

with
Code:
printf "Printing Array: %s\n" "${a#u}"

Your latter example should work with this script, using python:
Code:
#!/bin/sh

printf "Parameter 1: %s\n" "$1"
MYARRAY=`echo "print ' '.join($1)" | python`

for a in $MYARRAY
do
   printf "Printing Array: %s\n" "${a}"
done

Both of these have been tested.

Andrew
Thank you Andrew however, both the solutions still do not work !! See the output below:

Code:
$ ./test.sh [u/tmp/file.js, u/var/test.txt, u/tmp/llo.rft]
Parameter 1: [u/tmp/file.js,
Printing Array: /tmp/file.js
[ ~]$ more test.sh
#!/bin/sh

FPATH=($1)

printf "Parameter 1: %s\n" "$FPATH"
FPATH=`echo "$FPATH" | sed 's/^\[//; s/,/ /g; s/\]$//'`

for a in $FPATH
do
   a=`expr "$a" : 'u\(.*\)$'`
   printf "Printing Array: %s\n" "${a}"
done

PFA snapshot: Image

Can you please check ? I'm using python 2.7.5 however, your non-pyton solution too does not work and yeild the same incorrect output as shared above.

Last edited by mohtashims; 08-09-2019 at 08:26 AM..
# 10  
Quote:
Originally Posted by mohtashims
Thank you Andrew however, both the solutions still do not work !! See the output below:

Code:
$ ./test.sh [u/tmp/file.js, u/var/test.txt, u/tmp/llo.rft]
Parameter 1: [u/tmp/file.js,
Printing Array: /tmp/file.js
[ ~]$ more test.sh
#!/bin/sh

FPATH=($1)

printf "Parameter 1: %s\n" "$FPATH"
FPATH=`echo "$FPATH" | sed 's/^\[//; s/,/ /g; s/\]$//'`

for a in $FPATH
do
   a=`expr "$a" : 'u\(.*\)$'`
   printf "Printing Array: %s\n" "${a}"
done

PFA snapshot: Image

Can you please check ? I'm using python 2.7.5 however, your non-pyton solution too does not work and yeild the same incorrect output as shared above.
First, you need to quote your input:
Code:
 ./test.sh "[u/tmp/file.js, u/var/test.txt, u/tmp/llo.rft]"

Second, don't use the shell array construct - do this instead:
Code:
#!/bin/sh

FPATH="$1"

printf "Parameter 1: %s\n" "$FPATH"
FPATH=`echo "$FPATH" | sed 's/^\[//; s/,/ /g; s/\]$//'`

for a in $FPATH
do
   a=`expr "$a" : 'u\(.*\)$'`
   printf "Printing Array: %s\n" "${a}"
done

Which shell are you using on AIX?
These 2 Users Gave Thanks to apmcd47 For This Post:
# 11  
Quote:
Originally Posted by apmcd47
First, you need to quote your input:
Code:
 ./test.sh "[u/tmp/file.js, u/var/test.txt, u/tmp/llo.rft]"

Second, don't use the shell array construct - do this instead:
Code:
#!/bin/sh

FPATH="$1"

printf "Parameter 1: %s\n" "$FPATH"
FPATH=`echo "$FPATH" | sed 's/^\[//; s/,/ /g; s/\]$//'`

for a in $FPATH
do
   a=`expr "$a" : 'u\(.*\)$'`
   printf "Printing Array: %s\n" "${a}"
done

Which shell are you using on AIX?
I suspect I may be doing something wrong !!

I must say this works fine even when invoked from Ansible, however I have tested this on a My personal Linux System.

I would only be able to test this on AIX after 4 days as we have long weekend.

Special thanks to user @apm and others Smilie
This User Gave Thanks to mohtashims For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #160
Difficulty: Easy
The Internet Autonomous Network Authority (IANA) is responsible for maintaining the official assignments of Internet port numbers.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trying to loop through folders and execute an existing python script.

I am trying to loop through lots and lots of folders and use the names of the folders to run a Python script which has parameters. E.g. -- setup_refs -n John -f England/London/Hackney/John -c con/con.cnf Normally to run `setup_refs` once from command line it's: `python setup_refs.py -n John... (3 Replies)
Discussion started by: Mr_Keystrokes
3 Replies

2. Shell Programming and Scripting

Shell script to loop and store in array

I'm trying to achieve the follwoinig with no luck. Find the directories that are greater than 50GB in size and pick the owner of the directory as I would like to send an alert notification. du -sh * | sort -rh 139G Dir_1 84G Dir_2 15G Dir_3 ls -l Dir_1 drwx------ 2... (3 Replies)
Discussion started by: 308002184
3 Replies

3. Shell Programming and Scripting

**python** unable to read the background color in python

I am working on requirement on spreadsheet in python scripting. I have a spreadsheet containing cell values and with background color. I am able to read the value value but unable to get the background color of that particular cell. Actually my requirement is to read the cell value along... (1 Reply)
Discussion started by: giridhar276
1 Replies

4. Shell Programming and Scripting

Unable to store "python --version" to a shell variable

Hi All, I need to get the version of python installed and store it in a variable for later use. Whereas it is printing on the console instead of storing to variable. I am able to store output of ls command in a variable. Please check the below code : root@myhost:/volumes/srini# cat... (4 Replies)
Discussion started by: srinivasan.neel
4 Replies

5. UNIX for Dummies Questions & Answers

Pass array to shell and print

How do i pass an array from test4.sh to a function in another shell script test5.sh, basically i am sourcing the test5.sh in test4.sh and printing the contents, but not working below are my trial scripts, please help, thank you. #!/bin/bash # /usr/local/dw/archive/test5.sh print_array() {... (5 Replies)
Discussion started by: Ariean
5 Replies

6. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

7. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

8. Shell Programming and Scripting

[Solved] Unable to call a python script from bash

Hi, I am trying to run a python script embedded in bash script. But is throwing me an error. Please help. Script: #!/bin/bash nohup /usr/bin/python /opt/web/http.py & Error: /usr/bin/python: can't open file '/opt/web/http.py': No such file or directory Please help me on this. (6 Replies)
Discussion started by: maddy26615
6 Replies

9. Shell Programming and Scripting

Unable to print dynamic arguments in for loop

Hi All, I want to pass few dynamic arguments to shell script. The number of arguments differ each time I call the script. I want to print the arguments using the for loop as below. But not working out. for (( i=1; i<=$#; i++ )) do echo $"($i)" done /bin/sh test.sh arg1 arg2 arg3 ... (1 Reply)
Discussion started by: laalesh
1 Replies

10. Shell Programming and Scripting

loop in array in python

Hi suppose in python I have a list(or array, or tuple, not sure the difference) How do I loop inside the size of array. The pseudo code is: a= for i = 1 to dim(a) print a end How to find the dimension in python? Also, anyone has a handbook to suggest so I can borrow from library (1 Reply)
Discussion started by: grossgermany
1 Replies

Featured Tech Videos