Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
Season's Greetings for Ravi_Mangaluru hailing fro Mangaluru, Karnataka, India.
I need a help from you on a shell Script for the following question
I have a file called states with names of 10 states like a,b,c,d,e,f,g, etc;
My aim is to read the file states line by line and produce the output as
A beautiful State a
A beautiful State b
A beautiful State c
A beautiful State d
A beautiful State e and so on for all states in the file.
This is how my script looks
While !EOF do
echo " A beautiful Cat $state"
done
But I am not getting the desired output
Please note: This script has to be done without using BASH. It has to be written using the sh
Please edit the above post to use [code] [/code] tags around your script so that it is easier to read. Then try running it through shellcheck to proof read it for errors.
It looks like you aim to use a while loop to read line by line, so it would help to review the syntax for that.
Then give it a try again and report back on the changes. Presumably it will work, since it looks close already. If not, report on the progress and we can provide feedback.
You don't have to write a script for this. A sed one-liner would be enough. See example 25. Insert five blank spaces at the beginning of each line in Sed One-Liners Explained.
Dear Forum Users
Season's Greetings for Ravi_Mangaluru hailing fro Mangaluru, Karnataka, India.
I need a help from you on a shell Script for the following question I have a file called states with names of 10 states like a,b,c,d,e,f,g, etc; My aim is to read the file states line by line and produce the output as
Code:
A beautiful State a
A beautiful State b
A beautiful State c
A beautiful State d
A beautiful State e and so on for all states in the file.
This is how my script looks
Code:
While !EOF do
echo " A beautiful Cat $state"
done
But I am not getting the desired output Please note: This script has to be done without using BASH. It has to be written using the sh Can someone help me in this?
Why don't you tell us what you ARE getting when you run your script, and how you're running it. There is a site call shellcheck.net that will point out some flaws in your script, but this sounds very much like a verbatim-homework question. And using capital letters for While and Cat won't get you far.
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181
Rep:
@Ravi_Mangaluru
You can try the following
Code:
$ cat ./4175705567.sh
#!/bin/sh
/usr/bin/awk -F',' '{
for(col=1;col<NF;col++)
printf( "A beautiful State %s\n", $col);
printf( "A beautiful State %s\n", $1);
}' 4175705567.txt
$ chmod +x ./4175705567.sh
$ ./4175705567.sh
A beautiful State a
A beautiful State b
A beautiful State c
A beautiful State d
A beautiful State e
A beautiful State f
A beautiful State g
A beautiful State a
$ /usr/bin/awk -F',' '{
for(col=1;col<NF;col++)
printf( "A beautiful State %s\n", $col);
printf( "A beautiful State %s\n", $1);
}' ./4175705567.txt
A beautiful State a
A beautiful State b
A beautiful State c
A beautiful State d
A beautiful State e
A beautiful State f
A beautiful State g
A beautiful State a
$ #Updated script. Character "a" was repeated in your input file. Handled that duplicate errors.
$ cat ./4175705567.sh
#!/bin/sh
if [ -f ./4175705567.txt ]
then
/usr/bin/awk -F',' '{
for(indx=1;indx<=NF;indx++)
{
if ( "" != $indx )
{
BeautifulState[$indx] = $indx;
}
}
}
END {
for(DispVal in BeautifulState)
{
printf( "A beautifule State %s\n", DispVal);
}
}' ./4175705567.txt
else
echo ls ./4175705567.txt
ls ./4175705567.txt
fi
$ ./4175705567.sh
A beautifule State a
A beautifule State b
A beautifule State c
A beautifule State d
A beautifule State e
A beautifule State f
A beautifule State g
$ awk -F',' '{
for(indx=1;indx<=NF;indx++)
{
if ( "" != $indx )
{
BeautifulState[$indx] = $indx;
}
}
}
END {
for(DispVal in BeautifulState)
{
printf( "A beautifule State %s\n", DispVal);
}
}' ./4175705567.txt
A beautifule State a
A beautifule State b
A beautifule State c
A beautifule State d
A beautifule State e
A beautifule State f
A beautifule State g
$
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.