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
./myscript mylogname
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
To expand on what Skrynesaver used in the code, the man page for egrep has this for egrep -v :
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:
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.
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...
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
You 're right, it's not clear what I said.
I start again.
This my_script.sh
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
Then i launch
./my_script list_file
2nd case i launch the script without list_file
./my_script
I would like my_script creates automatically list_file with these comments
And at the prompt echo
Third case
I launch the script with these parameters
./my_script 10 20 srv_log srv4
I would like my_script creates automatically list_file with these comments.
I hope I was clear enough now
I'm sorry again. Thank you for your response.
.
More guessing than reading/understanding, I've come up with
#!/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
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 ")"
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 ")"?
#!/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
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.