LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 02-12-2013, 02:38 PM   #1
mikem1034
LQ Newbie
 
Registered: Feb 2013
Posts: 5

Rep: Reputation: Disabled
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.
 
Old 02-12-2013, 02:45 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by mikem1034 View Post
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.
 
Old 02-12-2013, 02:56 PM   #3
mikem1034
LQ Newbie
 
Registered: Feb 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
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.
 
Old 02-12-2013, 03:11 PM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
the first argument is $1, $0 is the name of the script itself
 
Old 02-12-2013, 03:29 PM   #5
mikem1034
LQ Newbie
 
Registered: Feb 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
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.
 
Old 02-12-2013, 04:10 PM   #6
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
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.
 
Old 02-12-2013, 11:28 PM   #7
mikem1034
LQ Newbie
 
Registered: Feb 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled

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"
 
Old 02-13-2013, 06:56 AM   #8
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
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.
 
Old 02-13-2013, 07:49 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
to create an empty file you can also use: touch <filename>
 
Old 02-13-2013, 08:03 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
It is not echo, it is printf:

Code:
printf 'Hello\n\tWorld\n'
or:

Code:
printf 'Hello\n\t%s\n' "$1"
 
Old 02-15-2013, 01:23 PM   #11
mikem1034
LQ Newbie
 
Registered: Feb 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script for run an shell script on server using ssh bloodstreetboy Linux - Server 5 01-12-2013 03:23 AM
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 09:12 PM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration