Create a file with comment in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create a file with comment in script
# 8  
Old 02-10-2013
Quote:
Originally Posted by RudiC
More guessing than reading/understanding, I've come up with
Code:
#!/bin/bash
# set -vx

f1()    {
         echo $1 $2 $3 $4                                       # put your function here
        } 


case $# in
        0)      echo -e "# comment1\n# comment2" > list_file    # create list_file as desired
                read -p "Please fill in this list_file: "       # prompt as desired, read answer
                echo $REPLY                                     # do whatever with the user's reply
                ;;
        1)      [ -f "$1" ] || exit                             # check if file exists; otherwise exit
                grep -v '^#' "$1" |                             # do what you did before
                        while read arg1 arg2 arg3 arg4
                                do f1 $arg1 $arg2 $arg3 $arg4
                                done
                ;;
        4)      echo -e "# comment1\n# comment2" > list_file    # create list_file as desired
                echo $1 $2 $3 $4 >> list_file                   # put your function here
                ;;
        *)      echo "error msg"; exit
esac

$ ./test X Y Z S
$ cat list_file
# comment1
# comment2
X Y Z S

Would this come near to what you expect?
Thank you very much RudiC, it' OK Smilie

I'd like to make a fourth case
Running the script as a parameter a file name at the discretion of the user

For example

Code:
./ my_script tom 10 20 srv_log srv4

Quote:
cat tom
# Comment 1
# begin_hour...end_hour.....name_log.....name_server
10 20 srv_log srv4
I tried in vain. Can you help me ?

Another question
How do I put begin_hour below 10, end_hour below 20 and so on ? (like an array)

I'm sorry but i'm not a pro in Shell
Thank you again
# 9  
Old 02-10-2013
Put this between the 4) line and the *) line:
Code:
        5)      echo -e "# comment1\n# comment2" > $1           # create file with $1 file name as desired
                shift
                echo $1 $2 $3 $4 >> list_file                   # put your function here
                ;;

Now that there's line after line with similar code coming up, we could find quite some opportunities for optimizing...
re. your "another question" - sorry, I don't understand what your after.
# 10  
Old 02-10-2013
Quote:
Originally Posted by RudiC
Put this between the 4) line and the *) line:
Code:
        5)      echo -e "# comment1\n# comment2" > $1           # create file with $1 file name as desired
                shift
                echo $1 $2 $3 $4 >> list_file                   # put your function here
                ;;

Now that there's line after line with similar code coming up, we could find quite some opportunities for optimizing...
re. your "another question" - sorry, I don't understand what your after.
It didn't work.
Is there an error at red line ? $1 instead of list_file
But it doesn't work too with echo $1 $2 $3 $4 >> $1
syntax error near unexpected symbol ")"



# 11  
Old 02-11-2013
Yes, you're absolutely right - should read >> "$1" (putting double quotes around $1 would be wise, too) Sorry for sloppy copying...
As there is no ")" found in the script text, did you check the contents of $1 for ")"?
# 12  
Old 02-18-2013
Quote:
Originally Posted by RudiC
Yes, you're absolutely right - should read >> "$1" (putting double quotes around $1 would be wise, too) Sorry for sloppy copying...
As there is no ")" found in the script text, did you check the contents of $1 for ")"?
Code:
#!/bin/bash
# set -vx

f1()    {
         echo $1 $2 $3 $4                                       # put your function here
        }


case $# in
        0)      echo -e "# comment1\n# comment2" > list_file    # create list_file as desired
                read -p "Please fill in this list_file: "       # prompt as desired, read answer
                echo $REPLY                                     # do whatever with the user's reply
                ;;
        1)      [ -f "$1" ] || exit                             # check if file exists; otherwise exit
                grep -v '^#' "$1" |                             # do what you did before
                        while read arg1 arg2 arg3 arg4
                                do f1 $arg1 $arg2 $arg3 $arg4
                                done
                ;;
        4)      echo -e "# comment1\n# comment2" > list_file    # create list_file as desired
                echo $1 $2 $3 $4 >> list_file                   # put your function here
                ;;
        5)      echo -e "# comment1\n# comment2" > $1           # create file with $1 file name as desired
                shift
                f1 $1 $2 $3 $4 >> "$1"                   # put your function here
                ;;

        *)      echo "error msg"; exit
esac

The case 5 didn't work.

I would like create a file named hello with comments and parameters like
Code:
cat hello
#comment1
#comment2
A B C D



I launch my script
Code:
./my_script hello A B C D



This script created 2 files hello and A

Code:
cat hello
# comment1
# comment2



Code:
cat A
A B C D


Thanks in advance for your help.
# 13  
Old 02-18-2013
Rats! Sorry for that. I should never give out any code without thoroughly testing it - even small "corrections" on the fly will come flying in my face!
Drop the shift and make the next line read
f1 $2 $3 $4 $5 >> "$1" Will work then, hopefully. Pls report back.
This User Gave Thanks to RudiC For This Post:
# 14  
Old 02-18-2013
Thank you RudiC Smilie it works.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove Comment Lines From Script/Input File

Hello, I have a SAS code that predominantly has comments line and the real code like below and i want to remove ONLY THE COMMENTS from the code in the single line or spanned across multiple lines. /******************************************************************** *** This Is a Comment... (4 Replies)
Discussion started by: arooonatr
4 Replies

2. Shell Programming and Scripting

Shell Script Comment code blocks in a bash source file

Just began to learn on Shell Script. I got an exercise from my friend. I know how to make this happen in C, but I'm not familiar with Shell Script. Hope I can get some help from all of you. I want to write a bash script to comment code blocks in a bash source file. What I mean comment is '#', I... (1 Reply)
Discussion started by: HiFuture0801
1 Replies

3. Shell Programming and Scripting

sed/awk script to replace only FIRST comment in the file

My first comment on every file contains the license message. I want to replace with a new license message. I used the below sed script, which replaces all comments. What is the modification or any other method with awk script for the below to edit only the first comment(license message)? #sed -f... (1 Reply)
Discussion started by: vpshastry
1 Replies

4. Shell Programming and Scripting

Create shell script to extract unique information from one file to a new file.

Hi to all, I got this content/pattern from file http.log.20110808.gz mail1 httpd: Account Notice: close igchung@abc.com 2011/8/7 7:37:36 0:00:03 0 0 1 mail1 httpd: Account Information: login sastria9@abc.com proxy sid=gFp4DLm5HnU mail1 httpd: Account Notice: close sastria9@abc.com... (16 Replies)
Discussion started by: Mr_47
16 Replies

5. Shell Programming and Scripting

Need to comment some code via script

Hi, I am writing a shell script, which should comment a if-fi loop in a file. From google i found that, we can use below qualifier to comment a section of code, :<<COMMENT COMMENT But i could not place these keywords before and after if-fi loop. Any help would be appreciated. Finally the... (2 Replies)
Discussion started by: successlin
2 Replies

6. UNIX for Dummies Questions & Answers

multiline comment in shell script

Is thery any way to give comment to multiple line without explicitly specifying # at the begining of each line ? (2 Replies)
Discussion started by: hiten.r.chauhan
2 Replies

7. Shell Programming and Scripting

Script to put block comment after finding regex in xml file

hi, i need my bash script to find regex in xml file.. and comment 2 lines before and after the line that contains regex.. can't use # needs to be <!-- at the beginning and --> and the end of the comment. so eg.. first block <filter> <filter-name>MyRegEx</filter-name> ... (11 Replies)
Discussion started by: Poki
11 Replies

8. UNIX for Dummies Questions & Answers

Why do a unix script starts with a comment?

For eg: #!/usr/bin/ksh <remaining code goes here> .. .. Does the compiler ignores that? Thanks (2 Replies)
Discussion started by: ajincoep
2 Replies

9. Shell Programming and Scripting

Pls comment on my script.

Hi guys, hope you scripting gurus here can help me out, the logic in my script somehow not working the way it should, this script part of a bigger backup script suppose to do some checking on the cluster prior to bringing up the package on MC/SG after backend cloning operation, this portion is... (3 Replies)
Discussion started by: sparcguy
3 Replies

10. Shell Programming and Scripting

Block Comment in Shell script

how to put multiline comments in a shell script like /* Some code */ in C language? (3 Replies)
Discussion started by: skyineyes
3 Replies
Login or Register to Ask a Question