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 starting my adventure in bash scripting, teaching myself. Now i've got alot of basics down, but there are a few things that are iffy, and I haven't came across each function in my tutorial. So here are my questions:
When echo -n "blah" is used, what purpose does -n server?
Using if and elif, what purpose do -z, -x, and ! = or if ! [] serve?
If you can toss any more - functions out there that will assist me that would be great.
Also, for my first simple program, I am coding an iptables script. I am wanting to have the user input several ports seperated by a space. i.e: 21 22 23 113
All in one line. How would I do this? $1 $2 $3 $4 and so on, or am I way off. I just started yesterday, and I am trying to catch on quickly. Thanks for the help guys.
For your iptables script, do you want to prompt the user for input after the program starts or do you want the user to start the program with the port data as arguments? Either are possible (or both - you could check if command line arguments were provided and if none are given, prompt the user for input).
read [-ers] [-u fd] [-t timeout] [-a aname] [-p prompt] [-n nchars]
[-d delim] [name ...]
One line is read from the standard input, or from the file
descriptor fd supplied as an argument to the -u option, and the
first word is assigned to the first name, the second word to
the second name, and so on, with leftover words and their
intervening separators assigned to the last name. If there are
fewer words read from the input stream than names, the remain-
ing names are assigned empty values. The characters in IFS are
used to split the line into words. The backslash character (\)
may be used to remove any special meaning for the next charac-
ter read and for line continuation. Options, if supplied, have
the following meanings:
-a aname
The words are assigned to sequential indices of the
array variable aname, starting at 0. aname is unset
before any new values are assigned. Other name argu-
ments are ignored.
-d delim
The first character of delim is used to terminate the
input line, rather than newline.
-e If the standard input is coming from a terminal, read-
line (see READLINE above) is used to obtain the line.
-n nchars
read returns after reading nchars characters rather than
waiting for a complete line of input.
-p prompt
Display prompt on standard error, without a trailing
newline, before attempting to read any input. The
prompt is displayed only if input is coming from a ter-
minal.
-r Backslash does not act as an escape character. The
backslash is considered to be part of the line. In par-
ticular, a backslash-newline pair may not be used as a
line continuation.
-s Silent mode. If input is coming from a terminal, char-
acters are not echoed.
-t timeout
Cause read to time out and return failure if a complete
line of input is not read within timeout seconds. This
option has no effect if read is not reading input from
the terminal or a pipe.
-u fd Read input from file descriptor fd.
If no names are supplied, the line read is assigned to the
variable REPLY. The return code is zero, unless end-of-file is
encountered, read times out, or an invalid file descriptor is
supplied as the argument to -u.
that is from man read (looks like it brings up a general bash programming man) Hope that helps.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.