Josh Pinto
Josh Pinto

Reputation: 1573

How to convert Python lists into a shell-readable series of strings?

I have this shell script (called by the file name depends) saved in my $PATH:

#!/bin/bash
for i in "$@"
do
  depends=+("nodejs-$i")
done
echo $depends

and the input (i.e., $@) it is designed to take is in the example format:

'flatten' 'once' 'is-file' 'multistream' 'piece-length' 'junk' 'xtend' 'bencode' 'readable-stream' 'run-parallel 'filestream' 'simple-sha1' 'minimist' 'block-stream2'

. So I would like to be able to write the dependencies listed in a package.json file, contained in the DEP list variable of this Python script (which has the file name npm2.py):

import json
from sys import argv
print(argv[1])
from subprocess import call

with open("/home/fusion809/OBS/home:fusion809:arch_extra/nodejs-" + argv[1] + "/src/package/package.json") as json_file:
    json_data = json.load(json_file)
    deps = json_data["dependencies"]
    LEN=len(deps)
    print(LEN)
    i=0
    DEP=list()
    print(DEP)
    for key, value in deps.items():
        print(key)
        DEP.append(key)
        i = i+1
        print(i)
        #call(["cpobsn", key, argv[1]])
    print(DEP)
    call("depends", DEP) # returns errors as DEP is a list!

to a new variable, say DEPS, so that it can then be used as input for the depends shell script. If it helps here is my current DEP variable that I would like to convert to the standard output shown previously:

['flatten', 'once', 'is-file', 'multistream', 'piece-length', 'junk', 'xtend', 'bencode', 'readable-stream', 'run-parallel', 'filestream', 'simple-sha1', 'minimist', 'block-stream2']

Hence I am here to ask how I can convert a Python list into shell-readable space-separated set of strings.

Upvotes: 0

Views: 333

Answers (1)

Robᵩ
Robᵩ

Reputation: 168596

subprocess.call takes its arguments in a single list. Try:

call(["depends"] + DEP)

Upvotes: 1

Related Questions