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
# 1  
Old 02-09-2013
Create a file with comment with a script

Hello,
Be indulgent for my english.

Can you help me ?

Code:
function f1 {


 } 
 egrep -v '^#' list_file \
 | while read arg1 arg2 arg3 arg4; do 
  f1 $arg1 $arg2 $arg3 $arg4   
done

In list_file there is
Quote:
# comment
# comment

10 20 12 9
I want to replace list_file by a $var
then when i launch the script with a file's name
For example
Code:
./my_script file1

I want the script create file1 with the #comment


Thank you in advance.

Last edited by amazigh42; 02-09-2013 at 04:07 AM.. Reason: title error
# 2  
Old 02-09-2013
I'm not sure if I follow your spec, however something like the following seems to be what you're trying to do:
Code:
skrynesaver@busybox ~/tmp$ cat tmp.sh 
#!/bin/bash
function f1 { echo  $(($1 + $2 + $3 + $4 )) ;  }

while read line ; do
	echo $line | grep -e '^#' >>$1 || f1 $line
done < ~/tmp/tmp.dat

skrynesaver@busybox ~/tmp$ cat tmp.dat
# comment 1
# comment 2
10 20 12 9
#comment 3
12 4 6 7 
#comment 4
skrynesaver@busybox ~/tmp$ ./tmp.sh comments
51
29
skrynesaver@busybox ~/tmp$ cat comments
# comment 1
# comment 2
#comment 3
#comment 4
skrynesaver@busybox ~/tmp$

# 3  
Old 02-09-2013
Thank you for your response but it is not exactly what I want.

I would like that the # comments are written inside the script itself.
These comments are only here to explain the array and are always the same.
The user enters only the four variables 10 20 srv_log srv4 from the third line.
The user can choice the name file.

For example

Code:
./myscript mylogname



Quote:
# Please enter the right variable
# begin_hour...end_hour.....name_log.....name_server

......10.............20..............srv_log...........srv4
Have a nice day..

---------- Post updated at 06:23 AM ---------- Previous update was at 06:17 AM ----------

I would like too that the script creates the mylogname when

./myscript mylogname

Last edited by amazigh42; 02-09-2013 at 07:21 AM.. Reason: addition tag
# 4  
Old 02-09-2013
To expand on what Skrynesaver used in the code, the man page for egrep has this for egrep -v :

Quote:
-v, --invert-match
Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX.)
So when you tried this:

egrep -v '^#' list_file
Egrep was told to search for all lines that did 'not' begin with a "#" character. Simply removing "-v" parameter would have allowed you to grep out the lines with hashes:

Code:
egrep '^#' list_file 
#!/bin/bash
# This is a comment
# This is another comment

The "-v" parameter works the same way with grep. As you may notice Skrynesaver used -e instead which is used for pattern matching like '^#', but essentially this is the same thing without the "-v".

Just thought I would add this in case you wanted to understand this and know more about how grep and egrep work.

EDIT: Sorry, if this seems out of order. I believe you must have posted while I was typing this.

Last edited by Azrael; 02-09-2013 at 07:31 AM.. Reason: Explaining
# 5  
Old 02-09-2013
It is really unclear what is required. Is the list_file variable or the output file?
You could try something like this and adjust as required...

Code:
f1() {
  :                             # put your function here
} 

[ $# -eq 1 ] || exit 1          # exit when no file provided
outfile=$1                      # this is the file that the user provides on the command line

while read line; do
  case $line in               
    \#*) echo "$line"           # Skip the comment
         ;;
      *) set -- $line           # split $line into positional parameters
         f1 "$@"                # call f1 with the positional parameters
         ;;
  esac
done < list_file > "$outfile"   # read from list_file, write to $outfile


Last edited by Scrutinizer; 02-09-2013 at 08:28 AM..
# 6  
Old 02-10-2013
Quote:
Originally Posted by Scrutinizer
It is really unclear what is required. Is the list_file variable or the output file?
You could try something like this and adjust as required...

Code:
f1() {
  :                             # put your function here
} 

[ $# -eq 1 ] || exit 1          # exit when no file provided
outfile=$1                      # this is the file that the user provides on the command line

while read line; do
  case $line in               
    \#*) echo "$line"           # Skip the comment
         ;;
      *) set -- $line           # split $line into positional parameters
         f1 "$@"                # call f1 with the positional parameters
         ;;
  esac
done < list_file > "$outfile"   # read from list_file, write to $outfile


Sorry Scrutinizer,

You 're right, it's not clear what I said.
I start again.

This my_script.sh

Code:
function f1 {


 } 
 egrep -v '^#' list_file \
 | while read arg1 arg2 arg3 arg4; do 
  f1 $arg1 $arg2 $arg3 $arg4   
done

For information
My function f1 is going to grep the lines between 2 hours (10 to 20) and (08 to 10) respectively in srv4's srv_log and srv3's srv_log.
Then this function f1 will put the results into 2 files named srv4.srv_log.10H-20H and srv3.srv_log.08H-10H.

Here's how I'd like my script works

1st case : I create the list_file before

Quote:
cat > list_file
10 20 srv_log srv4
08 10 srv_log srv3

Ctrl D
Then i launch
Code:
./my_script list_file

2nd case i launch the script without list_file
Code:
./my_script

I would like my_script creates automatically list_file with these comments
Quote:
# Please enter the right variables
# begin_hour...end_hour.....name_log.....name_server

And at the prompt echo

Quote:
"Please fill in this file list_file"
Third case
I launch the script with these parameters
Code:
./my_script 10 20 srv_log srv4

I would like my_script creates automatically list_file with these comments.
Quote:
# Please enter the right variables
# begin_hour...end_hour.....name_log.....name_server

10 20 srv_log srv4
I hope I was clear enough now
I'm sorry again. Thank you for your response.
.
# 7  
Old 02-10-2013
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?
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