Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-12-2013, 02:38 PM
|
#1
|
|
LQ Newbie
Registered: Feb 2013
Posts: 5
Rep: 
|
Shell script
Hi!
I need to create a script that will create a file and dump formatted text into it. I need to run the script with an argument which will be the filename. Also, I would like if there is no argument then a simple help screen will display.
Code:
#!/bin/sh
echo "Hello world" > ../directory/$arg1.txt
I need the text added to the file to be formatted like:
Code:
class Arg1 {
public function x() {
....
}
}
Can anybody help me create a script to do this?
Last edited by mikem1034; 02-12-2013 at 02:39 PM.
|
|
|
|
02-12-2013, 02:45 PM
|
#2
|
|
Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 11,778
|
Quote:
Originally Posted by mikem1034
Hi!
I need to create a script that will create a file and dump formatted text into it. I need to run the script with an argument which will be the filename. Also, I would like if there is no argument then a simple help screen will display.
Code:
#!/bin/sh
echo "Hello world" > ../directory/$arg1.txt
I need the text added to the file to be formatted like:
Code:
class Arg1 {
public function x() {
....
}
}
Can anybody help me create a script to do this?
|
Sure...post what you've written/done so far, and where you're stuck, and we'll be happy to help.
|
|
|
|
02-12-2013, 02:56 PM
|
#3
|
|
LQ Newbie
Registered: Feb 2013
Posts: 5
Original Poster
Rep: 
|
Quote:
Originally Posted by TB0ne
Sure...post what you've written/done so far, and where you're stuck, and we'll be happy to help.
|
This is all I have so far:
Code:
#!/bin/sh
if($# == 0)
// display help here
else
echo "class $0 { \n\n fumction x()" > ../directory/$0.txt
If I have more text this would probably not be the best way to add or format it.
Last edited by mikem1034; 02-12-2013 at 02:57 PM.
|
|
|
|
02-12-2013, 03:11 PM
|
#4
|
|
Senior Member
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 2,745
|
the first argument is $1, $0 is the name of the script itself
|
|
|
|
02-12-2013, 03:29 PM
|
#5
|
|
LQ Newbie
Registered: Feb 2013
Posts: 5
Original Poster
Rep: 
|
Quote:
Originally Posted by pan64
the first argument is $1, $0 is the name of the script itself
|
Thank you.
I meant to write:
Quote:
#!/bin/sh
if[$# -eq 0] then
# display help??
fi
echo "class $1 { \n\n fumction x()" > $1.txt
|
I seem to have an error in the if and the formatting is still a problem if I have a lot more text in that string with "\n" etc.
Also, what other error checking could I add?
Last edited by mikem1034; 02-12-2013 at 03:33 PM.
|
|
|
|
02-12-2013, 04:10 PM
|
#6
|
|
Member
Registered: Aug 2008
Distribution: Slackware
Posts: 173
Rep:
|
Shell tries to interpret the whole “if[$#” as a command name. (More precisely, $# is changed with the number of arguments). The same goes for “0]” which shell will interpret as an argument. Finally, “then” will be taken as yet another argument, whereas you wanted to use if's syntax here. The proper way is:
Code:
if [ $# -eq 0 ]; then
Take a look at specification for exact description.
As for the echo at the end, first of all, “\n”s won't be interpreted (without -e switch), but that's fine since instead of using echo you can use cat:
Code:
cat >$1.txt <<EOF
class $1 {
public function x() {
/* … */
}
};
EOF
Again, you can take a look at the specification to read more about here-document.
|
|
|
|
02-12-2013, 11:28 PM
|
#7
|
|
LQ Newbie
Registered: Feb 2013
Posts: 5
Original Poster
Rep: 
|
Thank you for that.
I ended up with something simple like:
Code:
#!/bin/sh
if [ $# -eq 0 ]; then
echo "\nUsage: ...\n"
fi
echo "
/**
*
*
*/
class $1
{
....
" > ../$1.txt
LOC="../$1/"
mkdir -p $LOC
echo "" > "$LOC index.php"
|
|
|
|
02-13-2013, 06:56 AM
|
#8
|
|
Member
Registered: Aug 2008
Distribution: Slackware
Posts: 173
Rep:
|
Quote:
|
if [ $# -eq 0 ]; then echo "\nUsage: ...\n"; fi
|
You probably want to “exit 1” after that echo.
Quote:
|
echo "" > "$LOC index.php"
|
What is that supposed to do? I guess you meant $LOC/index.php.
By the way, if you want to create an empty file, you can do “: >file-name”. echo will output a single new line. Also, specifying an empty argument for echo is redundant.
|
|
|
|
02-13-2013, 07:49 AM
|
#9
|
|
Senior Member
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 2,745
|
to create an empty file you can also use: touch <filename>
|
|
|
|
02-13-2013, 08:03 AM
|
#10
|
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,033
|
It is not echo, it is printf:
Code:
printf 'Hello\n\tWorld\n'
or:
Code:
printf 'Hello\n\t%s\n' "$1"
|
|
|
|
02-15-2013, 01:23 PM
|
#11
|
|
LQ Newbie
Registered: Feb 2013
Posts: 5
Original Poster
Rep: 
|
Quote:
Originally Posted by NevemTeve
It is not echo, it is printf:
Code:
printf 'Hello\n\tWorld\n'
or:
Code:
printf 'Hello\n\t%s\n' "$1"
|
Okay, thanks for the printf tip.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:54 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|