Hello,
How can i put a function into a condition ?
g1 is my function
I tested this in vain
if [ g1 $1 $2 $3 -ne 0 ];then
if [ $(g1 $1 $2 $3) -ne 0 ];then
Thanks for yours suggestions
Hello,
How can i put a function into a condition ?
g1 is my function
I tested this in vain
if [ g1 $1 $2 $3 -ne 0 ];then
if [ $(g1 $1 $2 $3) -ne 0 ];then
Thanks for yours suggestions
I think function's in bash doesn't accept any variables.
So it should better to define those values before you call your function.
sth like..
function g1 () {
echo $((a+b+c))
}
a=3
b=4
c=5
if [ $(g1) -ne 0 ];then
g1 #do what you want to do..
fi
Regards,
pamu
Thanks pamu for your help
Whatever i want, if there is a bad parameter in the input file, the function f1 should not process the line with a bad parameter.
I put your suggestion in red but i have this return code.
line 58: -ne: command not found
line 58: [: missing `]'
I notify this line in green in the script.
cat file
2013-03-31 09 23
2013-03-31 09 23
#2013-03-31 09 32
./myscript file
#!/bin/sh
#########################################################################
DATE=$1
HOUR=$2
MN=$3
function g1 () {
EC=0
case "$1" in
([1-9][0-9][0-9][0-9]-0[1-9]-0[1-9] | [1-9][0-9][0-9][0-9]-0[1-9]-1[0-9] | [1-9][0-9][0-9][0-9]-0[1-9]-2[0-9] | [1-9][0-9][0-9][0-9]-0[1-9]-3[0-1] | [1-9][0-9][0-9][0-9]-1[0-2]-0[1-9] | [1-9][0-9][0-9][0-9]-1[0-2]-1[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-2[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-3[0-1])
# nothing, OK !
;;
(*)
echo 'Fatal, $1 = '"'$1'"', Date not conform OR absent' >&2
EC=1
;;
esac
case "$2" in
([01][0-9] | 2[0-3])
# nothing, OK !
;;
(*)
echo 'Fatal, $2 = '"'$2'"', Hour in 2 digits between 00 and 23' >&2
EC=1
;;
esac
case "$3" in
([01][0-9] | [2-5][0-9])
# nothing, OK !
;;
(*)
echo 'Fatal, $3 = '"'$3'"', Minute in two digits between 00 and 59' >&2
EC=1
;;
esac
[ "$EC" = 1 ]
}
f1() {
echo -e "$DATE $HOUR $MIN"
}
case $# in
0) echo -e "# comment1\n# comment2" > list_file
read -p "Please fill in this list_file: "
echo $REPLY
;;
1) if [ ! -f "$1" ]
then > $1 && echo -e "# comment1\n# comment1" > $1
read -p "Please fill in "$1": "
echo $REPLY
else
if [ -f "$1" -a $(grep -v '^#' "$1" | wc -l ) -ne 0 | $(g1 $arg1 $arg2 $arg3) -ne 0 ] # line 58
then
grep -v '^#' "$1" |
while read arg1 arg2 arg3
do f1 $arg1 $arg2 $arg3
done
else
echo "Errors found in date/time - exiting now" >&2
exit 1
fi
read -p "Please fill in "$1": "
echo $REPLY
fi
;;
3) echo -e "# Fill in the 3 variables below\n# DATE HOUR MN" > $1
echo $2 $3 $4 >> "$1" # concatenation of variables in $ 1
grep -v '^#' "$1" | # Omits the comments
while read arg1 arg2 arg3 # Parse the parameters
do f1 $arg1 $arg2 $arg3 # Processing parameters for the function f1
done
;;
*) echo "error msg"; exit
esac
Incorrect, they take parameters just fine.
You call them directly, outside , like you would an external program.
if g1 $1 $2 $3
then
...
else
...
fi
And your function should use return to give an integer value that if can use in this manner. It's like exit but only quits a function or sourced script instead of the entire program. Zero means success, nonzero means failure, just like the exit status of an external utility. Your EC variable trick works but such indirectness isn't necessary.
g1 () {
if [ something ]
then
echo "success"
return 0
else
echo "failure"
return 1
fi
}
Thank you Corona688
I follow yours instructions.
I put an error in the first line (13 for the month)
cat inputfile
2013-13-31 12 05
2012-12-01 05 24
2011-02-21 09 15
When i launch the script, there are malfunctions.
1) Each line with right or wrong paramaters are printed twice.
I would like the line that has an error is not edited. It should only return the error code.
And others to be edited once.
Can you correct this script ?
Thank you in advance.
./my_script inputfile
Fatal, $1 = '2013-13-31', Date not conform OR absent
Errors found in date/time - exiting now
2013-13-31 12 05 line 1
2012-12-01 05 24 line 2
2011-02-21 09 15 line 3
2013-13-31 12 05 line 1
2012-12-01 05 24 line 2
2011-02-21 09 15 line 3
#!/bin/sh
#########################################################################
g1() {
case "$1" in
([1-9][0-9][0-9][0-9]-0[1-9]-0[1-9] | [1-9][0-9][0-9][0-9]-0[1-9]-1[0-9] | [1-9][0-9][0-9][0-9]-0[1-9]-2[0-9] | [1-9][0-9][0-9][0-9]-0[1-9]-3[0-1] | [1-9][0-9][0-9][0-9]-1[0-2]-0[1-9] | [1-9][0-9][0-9][0-9]-1[0-2]-1[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-2[0-9] | [1-9][0-9][0-9][0-9]-1[0-2]-3[0-1])
# RAS, OK !
return 0;
;;
(*)
echo 'Fatal, $1 = '"'$1'"', Date not conform OR absent' >&2
;;
esac
return 1
case "$2" in
([01][0-9] | 2[0-3])
# nothing, OK !
return 0;
;;
(*)
echo 'Fatal, $2 = '"'$2'"', Hour 2 digits between 00 and 23' >&2
;;
esac
return 1
case "$3" in
([01][0-9] | [2-5][0-9])
# nothing, OK !
return 0;
;;
(*)
echo 'Fatal, $3 = '"'$3'"', Minute in two digits between 00 and 59' >&2
esac
return 1
}
f1() {
echo $1 $2 $3 "line" $4
#[ $1 = $1 ]
}
case $# in
0) echo -e "# comment\n# commentaire2" > list_file
read -p "Please fill in this list_file: "
echo $REPLY
;;
1) if [ ! -f "$1" ]
then > $1 && echo -e "# comment\n# comment" > $1
read -p "Please fill in "$1": "
echo $REPLY
else
if [ -f "$1" -a $(grep -v '^#' "$1" | wc -l ) -ne 0 ]
then
grep -v '^#' "$1" |
while read arg1 arg2 arg3
do
if ! g1 $arg1 $arg2 $arg3
then
echo "Errors found in date/time - exiting now" >&2
#exit 1
else
grep -v '^#' "$1" |
while read arg1 arg2 arg3
do f1 $arg1 $arg2 $arg3 $((++line))
done
fi
done
else
read -p "Please fill in "$1": "
echo $REPLY
fi
fi
;;
*) echo "error msg"; exit
esac
Do not PM people to bring them back faster. PMing people for technical support is against the rules.
That program is so big and messy I can't tell what you're trying to do, let alone what's wrong with it. Suspect there's a far simpler way to do what you want, whatever it is. Please explain its purpose.
f1() {
echo $1 $2 $3 "line" $4
Here f1 is a simple function to test the function g1.
g1 is a function which tests the parameters format of an input file.
Theses parameters are YEAR BEGIN_hour END_hour.
Month must be between 1 and 12
Day must be between 1 and 31
Hour must be between 00 and 23 (2 digits)
Minutes must be between 00 and 59 (2 digits)
case 0)
If the script is launched without argument, an inputfile named list_file will be created with comments.
The main engine ie case 1)
If the inputfile exists, the script tests if there are parameters.
Then it parses each parameters to check the validity of the format
When i launch the script, there are malfunctions.
1) Each line with right or wrong parameters are printed twice.
I would like the line that has an error is not printed. It should only return the error code.
And others to be edited once.
I hope to be clearer.
Have a nice day.
case $# in
0) echo -e "# comment1\n# comment2" > list_file
read -p "Please fill in this list_file: "
echo $REPLY
;;
1) if [ ! -f "$1" ]
then > $1 && echo -e "# comment1\n# comment1" > $1
read -p "Please fill in "$1": "
echo $REPLY
else
if [ -f "$1" -a $(grep -v '^#' "$1" | wc -l ) -ne 0 | $(g1 $arg1 $arg2 $arg3) -ne 0 ] # line 58
then
grep -v '^#' "$1" |
while read arg1 arg2 arg3
do f1 $arg1 $arg2 $arg3
done
else
echo "Errors found in date/time - exiting now" >&2
exit 1
fi
read -p "Please fill in "$1": "
echo $REPLY
fi
;;
3) echo -e "# Fill in the 3 variables below\n# DATE HOUR MN" > $1
echo $2 $3 $4 >> "$1" # concatenation of variables in $ 1
grep -v '^#' "$1" | # Omits the comments
while read arg1 arg2 arg3 # Parse the parameters
do f1 $arg1 $arg2 $arg3 # Processing parameters for the function f1
done
;;
*) echo "error msg"; exit
esac
Which one are hours?
Which one are days?
Which one are minutes?
What does your paramater file look like?
Etc.
Also, that date does look invalid since there's no 13th month.
A date is invalid if month is greater than 12 or day is greater than 31 or written in one digit or if the separator is different from dash -
An hour is invalid if it is greater than 23 or written in one digit.
A minute is invalid if it is greater than 59 or written in one digit.
Do I have answered all your questions ?
./my_script inputfile
My parameters file look like this
cat inputfile
# YEAR HOUR MN
2013-03-31 09 23
2013-03-31 09 32
Dumping it all into one huge case just makes it hard to fix, and your program can be a lot simpler. And your functions should have names that have something to do with what they're for, not things like 'g1'.
valid() {
case "$3" in
[0-5][0-9]) ;;
60) ;;
*) return 1;
;;
case "$2" in
[0-1][0-9]) ;;
2[0-3]) ;;
*) return 1 ;;
esac
OLDIFS="$IFS"
IFS="-"
set -- $1 # $1=YYYY, $2=MM, $3=DD
IFS="$OLDIFS"
case "$1" in
[0-9][0-9][0-9][0-9]) ;;
*) return 1;
esac
case "$2" in
0[1-9]) ;;
1[0-2]) ;;
*) return 1;;
esac
case "$3" in
0[1-9]) ;;
[1-2][0-9]) ;;
3[0-1]) ;;
*) return 1 ;;
esac
return 0
}
while read DATE HH MM
do
[ "${DATE:0:1}" = "#" ] && continue # Ignore comments
[ -z "$DATE" ] && continue # Ignore blank lines
valid $DATE $HH $MM || echo "$DATE $HH $MM is invalid"
done < inputfile
Thanks a lot, it's clearer and easier to read. You take a weight off my mind.
Which partly solves my problems as the badly formatted lines are reported once.
But I am facing a problem, I can not create an another function that processes only lines well formatted. I continue to look, but all suggestions are welcome.
Another thing
Can you explain the red tag in script ?
valid() {
case "$3" in
[0-5][0-9]) ;;
#60) ;;
*) return 1;
;;
esac
case "$2" in
[0-1][0-9]) ;;
2[0-3]) ;;
*) return 1 ;;
esac
OLDIFS="$IFS"
IFS="-"
set -- $1 # $1=YYYY, $2=MM, $3=DD
IFS="$OLDIFS"
case "$1" in
[0-9][0-9][0-9][0-9]) ;;
*) return 1;
esac
case "$2" in
0[1-9]) ;;
1[0-2]) ;;
*) return 1;;
esac
case "$3" in
0[1-9]) ;;
[1-2][0-9]) ;;
3[0-1]) ;;
*) return 1 ;;
esac
return 0
}
while read DATE HH MM
do
[ "${DATE:0:1}" = "#" ] && continue # Ignore comments
[ -z "$DATE" ] && continue # Ignore blank lines
valid $DATE $HH $MM || echo "$DATE $HH $MM is invalid"
done < inputfile
I am facing a problem, I can not create a function that processes only lines well formatted. I continue to look, but all suggestions are welcome.
${DATE:0:1} that's a substring operator -- the variable, the offset, and the length. An offset of 0 and length of 1 gets the first character.
To only process valid lines:
valid () {
...
}
process() {
...
}
while read DATE HH MM
do
[ "${DATE:0:1}" = "#" ] && continue # Ignore comments
[ -z "$DATE" ] && continue # Ignore blank lines
if ! valid $DATE $HH $MM # Ignore invalid lines
then
echo "$DATE $HH $MM is invalid"
continue
fi
process $DATE $HH $MM
done < inputfile
Hello,
I try to understand your method.
This is why I added other cases.
But the script returns an error on a line that has good parameters.
Please, can you explain why ?
Another question :
The pattern in red means that from there, we must consider the variables starting from $ 1
Have a nice day
cat inputfile
QCVD UPG8 22 16 server 2013-01-24
./my_script
QCVD UPG8 22 16 server 2013-01-24 is invalid
#!/bin/bash
#########################################################################
valid() {
case "$1" in
[E,F,Q,P][A,C,S][A,I,V,P][D,R,T,V]) ;;
*) return 1 ;;
esac
case "$2" in
[PU][IPS][AGS][1-8]) ;;
ORA[1-8]) ;;
*) return 1 ;;
esac
case "$3" in
[0-1][0-9]) ;;
2[0-3]) ;;
*) return 1 ;;
esac
case "$4" in
[0-5][0-9]) ;;
*) return 1 ;;
esac
case "$5" in
server) ;;
exploitation) ;;
*) return 1 ;;
esac
OLDIFS="$IFS"
IFS="-"
set -- $1 # $1=YYYY, $2=MM, $3=DD
IFS="$OLDIFS"
case "$1" in
[2-9][0-9][0-9][0-9]) ;;
*) return 1 ;;
esac
case "$2" in
0[1-9]) ;;
1[0-2]) ;;
*) return 1 ;;
esac
case "$3" in
0[1-9]) ;;
[1-2][0-9]) ;;
3[0-1]) ;;
*) return 1 ;;
esac
return 0
}
process() {
echo "$CONTEXTE $INSTANCE $HH $MM $type_log $DATE"
}
while read CONTEXTE INSTANCE HH MM type_log DATE
do
[ "${DATE:0:1}" = "#" ] && continue # Ignore comments
[ -z "$DATE" ] && continue # Ignore blank lines
if ! valid $CONTEXTE $INSTANCE $HH $MM $type_log $DATE # Ignore invalid lines
then
echo "$CONTEXTE $INSTANCE $HH $MM $type_log $DATE is invalid"
continue
fi
process $CONTEXTE $INSTANCE $HH $MM $type_log $DATE
done < inputfile
---------- Post updated at 07:49 AM ---------- Previous update was at 04:34 AM ----------
The red pattern was the source of my problem.
I have changed $1 in $6
Thanks a lot Mister Corona688
Marvelous forum where i learn a lot.
OLDIFS="$IFS"
IFS="-" set -- $6 # $1=YYYY, $2=MM, $3=DD
IFS="$OLDIFS"
Your config file is very different from what you said it was:
QCVD UPG8 ...
Since you didn't tell me those were there, I was using the wrong tokens.
As for what it does, the comment says it all:
set -- $1 # $1=YYYY, $2=MM, $3=DD
It alters the values of $1, $2, ... based on what you input into it. It splits upon IFS, so altering IFS lets me split on "-" instead of space. Put the wrong thing into it, get the wrong thing out.
Hello,
Thanks for yours explainations.
I have another question if you have some time.
I tried that ie put the arguments after the command
./ my_script QCVD UPG8 22 16 server 2013-01-24
What should I change to make it work in magenta ?
#!/bin/bash
#########################################################################
valid() {
case "$1" in
[E,F,Q,P][A,C,S][A,I,V,P][D,R,T,V]) ;;
*) return 1 ;;
esac
case "$2" in
[PU][IPS][AGS][1-8]) ;;
ORA[1-8]) ;;
*) return 1 ;;
esac
case "$3" in
[0-1][0-9]) ;;
2[0-3]) ;;
*) return 1 ;;
esac
case "$4" in
[0-5][0-9]) ;;
*) return 1 ;;
esac
case "$5" in
server) ;;
exploitation) ;;
*) return 1 ;;
esac
OLDIFS="$IFS"
IFS="-"
set -- $1 # $1=YYYY, $2=MM, $3=DD
IFS="$OLDIFS"
case "$1" in
[2-9][0-9][0-9][0-9]) ;;
*) return 1 ;;
esac
case "$2" in
0[1-9]) ;;
1[0-2]) ;;
*) return 1 ;;
esac
case "$3" in
0[1-9]) ;;
[1-2][0-9]) ;;
3[0-1]) ;;
*) return 1 ;;
esac
return 0
}
process() {
echo "$CONTEXTE $INSTANCE $HH $MM $type_log $DATE"
}
while read CONTEXTE INSTANCE HH MM type_log DATE
do
[ "${DATE:0:1}" = "#" ] && continue # Ignore comments
[ -z "$DATE" ] && continue # Ignore blank lines
if ! valid $CONTEXTE $INSTANCE $HH $MM $type_log $DATE # Ignore invalid lines
then
echo "$CONTEXTE $INSTANCE $HH $MM $type_log $DATE is invalid"
continue
fi
process $CONTEXTE $INSTANCE $HH $MM $type_log $DATE
done
Well, the 'while read' loop and command reads from stdin. If you don't want to read from stdin, don't.
The parameters will be available as $1 $2 ... so just call your function with these options
Hello,
Sorry, I don't speak fluent English and I don't understand all the idiomatic forms. I don't quite understand your suggestions.
But i tried that.
Thank you in advance.
process() {
echo "$1 $2 $3 $4 $5 $6"
}
if ! valid $1 $2 $3 $4 $5 $6 # Ignore invalid lines
then
echo "$1 $2 $3 $4 $5 $6 is invalid"
else
process $1 $2 $3 $4 $5 $6
fi
It doesn't work, i have the same result invalid.
With good arguments
./my_script 2013-03-13 22 25 server
2013-03-13 22 25 server is invalid
With bad argument in red
./my_script 2013-03-35 22 25 server
2013-03-35 22 25 server is invalid
Your arguments are totally different from your config file again and the function will have to be altered.
There's no QCVD UPG8 for instance.
Hello,
Sorry, this is an unfortunate copy-paste.
It's OK now
Have a nice day.
#!/bin/bash
#########################################################################
valid() {
case "$1" in
[E,F,Q,P][A,C,S][A,I,V,P][D,R,T,V]) ;;
*) return 1 ;;
esac
case "$2" in
[PU][IPS][AGS][1-8]) ;;
ORA[1-8]) ;;
*) return 1 ;;
esac
case "$3" in
[0-1][0-9]) ;;
2[0-3]) ;;
*) return 1 ;;
esac
case "$4" in
[0-5][0-9]) ;;
*) return 1 ;;
esac
case "$5" in
server) ;;
exploitation) ;;
*) return 1 ;;
esac
OLDIFS="$IFS"
IFS="-"
set -- $1 # $1=YYYY, $2=MM, $3=DD
IFS="$OLDIFS"
case "$1" in
[2-9][0-9][0-9][0-9]) ;;
*) return 1 ;;
esac
case "$2" in
0[1-9]) ;;
1[0-2]) ;;
*) return 1 ;;
esac
case "$3" in
0[1-9]) ;;
[1-2][0-9]) ;;
3[0-1]) ;;
*) return 1 ;;
esac
return 0
}
process() {
echo "$1 $2 $3 $4 $5 $6"
}
if ! valid $1 $2 $3 $4 $5 $6 # Ignore invalid lines
then
echo "$1 $2 $3 $4 $5 $6 is invalid"
else
process $1 $2 $3 $4 $5 $6
fi