0

I know this question is basic knowing how to write script, but I didn't learn that yet:

I have a program that takes, as an input, a list like: str_1,str_2, etc. The string is always the same, but the numbers go from 1 to 1,000. Obviously, it is not possible to write this manually, so I want to generate the argument str_1,str_2, etc automatically and then copy it to the shell (or, if there is a better way to do, please let me know).

How can I generate this list?

  • 2
    It depends on the application, do you call it via the shell, with the string as argument? In principal, in python this would be a peace of cake. – Jacob Vlijm Mar 6 '14 at 9:25
  • yes this is what I do. which library would you use in python to do this? – bigTree Mar 6 '14 at 9:30
  • 1
    to make sure: the command is: application_name string? – Jacob Vlijm Mar 6 '14 at 9:32
  • that is correct: python programName arguments (the first argument being the string) – bigTree Mar 6 '14 at 9:37
  • excuse me, having a little difficulties pasting code :) – Jacob Vlijm Mar 6 '14 at 9:56
1
#!/usr/bin/python3

import os

def create_list(string_input):
    number = 0
    string_list = []
    while number <= 1000:
        number = number+1
        string_list.append(string_input+str(number))
    return(string_list)

command_list = create_list(string_input = "test")
for item in command_list:
    os.system("firefox -new-tab "+item)

This would run firefox 1000 times, opening new tabs with "test1, test2" as url (don't try :))

change "string_input" to your string, "firefox -new-tab" to your application.

edit: I must have had a weak moment, can be much shorter in python as well of course:

#!/usr/bin/python3

import subprocess

command_list = [
    "firefox -new-tab "+"test"+str(number) for number in range(0,1001)
    ]
for command in command_list:
    subprocess.call(command)
| improve this answer | |
  • 1
    btw, I the script is in python3 – Jacob Vlijm Mar 6 '14 at 10:11
  • Python is probably overkill for this task. Also, the os.system call is superseded by subprocess.call(). Also, commands should be specified in a list. – kiri Mar 7 '14 at 22:47
1
# array=( str_1 str_2 str_3 ... str_1000 )
array=( str_{1..1000} )          

# prints the content of the array as a single string with comma between elements. 
# str_1,str_2,str_3,...,str_1000   
( IFS=,; printf '%s\n' "${array[*]}"; ) 

See http://mywiki.wooledge.org/BashFAQ/005 for more on using arrays in bash.

| improve this answer | |
  • That looks nice! I will look into that, thanks for the link! – Jacob Vlijm Mar 6 '14 at 10:16
  • ./mystring.bash $(tt=$(echo str_{1..1000}) ; echo ${tt// /,}) – c4f4t0r Mar 6 '14 at 13:15
1

This will work nicely in Bash only:

array=( str_{1..1000} )
joined_array="$(printf '%s,' "${array[@]}")"
echo "${joined_array%,}"

The last echo line is to omit the trailing comma.

First line of the commands is from geirha's answer.

| improve this answer | |
0

There are, as always, plenty of solutions.

Personally, for such a simple thing as this, neither do I write a whole Python script, nor do I declare arrays in bash or change the internal field separator.

Instead I just write something like this, right from the command line, in one line:

for i in {1..1000}; do printf str_$i,; done

This will output a string str_1,str_2,...,str_1000,. Depending on the level of automation you need, if you would like to strip the trailing , then one of an estimated gajillion possibilities is to use sed:

for i in {1..1000}; do printf str_$i,; done | sed 's/,$//;'

giving you str_1,str_2,...,str_1000.

Update:

It just came to me that since you only want to do a print, you can even do without an explicit loop. Short & crisp:

echo str_{1..1000} | sed 's/ /,/g'

The latter command also gives you str_1,str_2,...,str_1000.

Explanation: The echo str_{1..1000} outputs str_1 str_2 ... str_1000. The sed 's/ /,/g' replaces all whitespaces by commas.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.