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.
Example: ./add_host eccws531 DEV "Perl Shared 1" linux
So:
eccws531 = $1
DEV = $2
linux SHOULD = $4 and "Perl Shared 1" SHOULD = $3, but the string is broken up as seperate arguments. I can't use shift, because this quoted string will vary in word length. Can I use getopts? I'm not very familiar with it so how would I use it to accomplish this?
I have a file that has over 800 hosts in it and when I run the ./add_host from the command line and supply the arguments by hand it parses correctly like you demonstrated.
However when I try to feed the list into the script in a for loop
for i in `cat docu.list`;do ./add_host $i;done
The docu.list entries look like:
eccws531 DEV "Perl Shared 1" linux (And so on)
It takes each word as a separate argument? Should I be doing this in a while loop instead?
You can do that. However, if you want to use for loop, just do double quotes
Code:
# more file
/home/path/file with spaces
/home/path/normalfile
# for item in `cat file`; do echo "$item"; done
/home/path/file
with
spaces
/home/path/normalfile
# for item in "`cat file`"; do echo "$item"; done
/home/path/file with spaces
/home/path/normalfile
You can do that. However, if you want to use for loop, just do double quotes
Code:
# more file
/home/path/file with spaces
/home/path/normalfile
# for item in `cat file`; do echo "$item"; done
/home/path/file
with
spaces
/home/path/normalfile
# for item in "`cat file`"; do echo "$item"; done
/home/path/file with spaces
/home/path/normalfile
I am failing...
eccws587:/tmp # more docu.list
eccas873 QA "WAS 6.1 SHARED FARM PHASE I" linux
Here's a simple awk script to get the different fields...
Code:
$ cat hosts.txt
eccws531 DEV "Perl Shared 1" linux
eccws531 DEV "Perl Shared 2" linux
eccws531 DEV "Perl Shared 3" linux
eccws531 DEV "Perl Shared 4" linux
eccws531 DEV "Perl Shared 5" linux
eccws531 DEV "Perl Shared 6" linux
eccws531 DEV "Perl Shared 7" linux
eccws531 DEV "Perl Shared 8" linux
eccws531 DEV "Perl Shared 9" linux
eccws531 DEV "Perl Shared 10" linux
eccws531 DEV "Perl Shared 11" linux
eccws531 DEV "Perl Shared 12" linux
eccws531 DEV "Perl Shared 13" linux
eccws531 DEV "Perl Shared 14" linux
eccws531 DEV "Perl Shared 15" linux
eccws531 DEV "Perl Shared 16" linux
Set the IFS at the top of the script to a newline only. Normally it breaks args on space OR tab OR newline. Note that you have to use a literal newline char eg
#start
IFS=<hit rtn key here>
#end
not
IFS=\n
with or without single/double quotes
alternately you can use
Code:
# How to read file line by line:
while read line
do
echo "$line"
done <data-file
Set the IFS at the top of the script to a newline only. Normally it breaks args on space OR tab OR newline. Note that you have to use a literal newline char eg
#start
IFS=<hit rtn key here>
#end
not
IFS=\n
with or without single/double quotes
alternately you can use
Code:
# How to read file line by line:
while read line
do
echo "$line"
done <data-file
Perhaps something like this? (re-quoting if necessary)
Code:
Big_Vern:~$ cat text
abc def "ghi jkl" mno
Big_Vern:~$ cat scr
while read line
do
echo $line
eval set -a $line
echo "No 1: $1"
echo "No 2: $2"
echo "No 3: $3"
echo "No 4: $4"
done <text
Big_Vern:~$ ./scr
abc def "ghi jkl" mno
No 1: abc
No 2: def
No 3: ghi jkl
No 4: mno
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.