LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Shell script (http://www.linuxquestions.org/questions/programming-9/shell-script-4175449868/)

mikem1034 02-12-2013 03:38 PM

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?

TB0ne 02-12-2013 03:45 PM

Quote:

Originally Posted by mikem1034 (Post 4890163)
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.

mikem1034 02-12-2013 03:56 PM

Quote:

Originally Posted by TB0ne (Post 4890167)
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.

pan64 02-12-2013 04:11 PM

the first argument is $1, $0 is the name of the script itself

mikem1034 02-12-2013 04:29 PM

Quote:

Originally Posted by pan64 (Post 4890188)
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?

mina86 02-12-2013 05:10 PM

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.

mikem1034 02-13-2013 12:28 AM


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"


mina86 02-13-2013 07:56 AM

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.

pan64 02-13-2013 08:49 AM

to create an empty file you can also use: touch <filename>

NevemTeve 02-13-2013 09:03 AM

It is not echo, it is printf:

Code:

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

Code:

printf 'Hello\n\t%s\n' "$1"

mikem1034 02-15-2013 02:23 PM

Quote:

Originally Posted by NevemTeve (Post 4890739)
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.


All times are GMT -5. The time now is 09:00 PM.