ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying to create a script that will output a string of comma separated numbers to the command line when given the first and last number. e.g.
% <scriptname> 12345 12350
% 12345,12346,12347,12348,12349,12350
I've been trying to do it with bash and perl but to no avail. Can anyone help?
#!/usr/bin/env sh
# @(#) s1 Demonstrate sequence of comma-separated integers with seq.
set -o nounset
echo
debug=":"
debug="echo"
## Use local command version for the commands in this demonstration.
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash seq
echo
seq -s, 12345 12350
exit 0
producing:
Code:
% ./s1
(Versions displayed with local utility "version")
GNU bash 2.05b.0
seq (coreutils) 5.2.1
12345,12346,12347,12348,12349,1235
...
Edit: you need zsh (or bashdiff, IIRC) if you want to
use parameters inside the braces:
...
I like the shell solution. Adding a spin with eval makes it work with bash3:
Code:
#!/usr/bin/env bash3
# @(#) s2 Demonstrate feature.
set -o nounset
echo
debug=":"
debug="echo"
## Use local command version for the commands in this demonstration.
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash3
echo
echo " bash3 with constants:"
(IFS=,;set -- {12345..12350};printf "%s\n" "$*")
# Original
# start=12345
# end=12350
# IFS=,;set -- {$start..$end};printf "%s\n" "$*"
start=12345
end=12350
echo
echo " bash3 with variables:"
IFS=,;set -- {$start..$end};printf "%s\n" "$*"
echo
echo " bash3 variables with eval:"
IFS=,;eval set -- {$start..$end};printf "%s\n" "$*"
exit 0
Producing:
Code:
% ./s2
(Versions displayed with local utility "version")
GNU bash 3.00.16(1)-release
bash3 with constants:
12345,12346,12347,12348,12349,12350
bash3 with variables:
{12345..12350}
bash3 variables with eval:
12345,12346,12347,12348,12349,12350
I set the output field separator to a comma then stuff $1...$N with the appropriate values and rely on awk concatenating things to create $0 for printing.
I set the output field separator to a comma then stuff $1...$N with the appropriate values and rely on awk concatenating things to create $0 for printing.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.