So I have a bash script that needs to take an arbitrary number of command line arguments and put them into a single string
Example of what the user would type in:
give <environment> <email> <any number of integers separated by spaces>
give testing stuff#things.com 1 2 3 4 5
I want to get all of the arguments from $3 to $# and concat them into a string.
My (probably awful) solution right now is
if [ $# -gt 3 ]
then
env="env="$1
email="email="$2
entList=""
for i in {3..$#}
do
if [ $i -eq 3 ]
then
entList=$3
shift
fi;
if [ $i -gt 3 ]
then
entList=$entList","$3
shift
fi;
done
fi;
I handle the case of having only three arguments a bit differently, and that one works fine.
Final value of $entList given the example give testing stuff#things.com 1 2 3 4 5 should be: 1,2,3,4,5
Right now when i run this i get the following Errors:
/usr/local/bin/ngive.sh: line 29: [: {3..5}: integer expression expected
/usr/local/bin/ngive.sh: line 34: [: {3..5}: integer expression expected
Lines 29 and 34 are:
line 29: if [ $i -eq 3 ]
line 34: if [ $i -gt 3 ]
Any help would be appreciated.
You're on the right track. Here's my suggestion:
if [ $# -ge 3 ]; then
env="$1"
email="$2"
entlist="$3"
while shift && [ -n "$3" ]; do
entlist="${entlist},$3"
done
echo "entlist=$entlist"
else
echo "Arguments: $*"
fi
Note that variables should always be put inside quotes. I'm not sure why you were setting env=env=$1, but I suspect that if you want to recycle that value later, you should do it programatically rather than by evaluating the variable as if it were a statement, in case that was your plan.
Skip first three arguments using a subarray:
all=( ${#} )
IFS=','
threeplus="${all[*]:3}"
The reason you're getting those error messages is that in:
for i in {3..$#}
The brace expansion is performed before the parameter expansion and so the following if statement is evaluated as:
if [ {3..$#} -eq 3 ]
which isn't valid.
Change your for statement to use the C style:
for ((i = 3; i <= $#; i++))
Use this style for integer comparison:
if (( $# > 3 ))
and
if (( i == 3 ))
and
if (( i > 3 ))
Put your parameters inside the quotes:
env="env=$1"
email="email=$2"
and
entList="$entList,$3"
although the quotes aren't necessary since word splitting isn't performed on the right side of an assignment and you're not assigning special characters such as whitespace, semicolons, pipes, etc.
Related
I am trying to generate a sequence of ones and zeros in using bash script.
#!/bin/bash
clock=1
n=1
# continue until $n equals 5
while [ $n -le 5 ]
do
echo "$clock"
n=$(( n+1 )) # increments $n
clock=$(~clock)
done
Expected output:
1
0
1
0
1
Output generated from the above code:
I am getting error from this line: clock=$(~clock)
Error: ~clock: command not found
If you want to generate sequence with 1 and 0 alternatively, you can use
#!/bin/bash
clock=1
n=1
# continue until $n equals 5
while [ $n -le 5 ]
do
echo "$clock"
n=$(( n+1 ))
# increments $n
clock=$((clock+1))
clock=$((clock%2))
done
If you want to generate random sequence of 1 and 0s, you can use
#!/bin/bash
n=1
# continue until $n equals 5
while [ $n -le 5 ]
do
echo "$((RANDOM%2))"
n=$(( n+1 ))
done
Problem is in this line:
clock=$(~clock)
Here bash is trying to run anything inside $(...) as a command (it is called command substitution).
Using ~clock is also incorrect as it will only do bitwise negation and will not produce 1 and 0 as you are expecting.
You can use this script to get alternate 1 and 0 printed:
#!/bin/bash
clock=1
# continue until $n equals 5
for ((n=0; n<5; n++))
do
echo "$clock"
clock=$((1 - clock))
done
Another short way to do it
#!/bin/bash
for i in {1..5}
do
echo $((i%2))
done
Or slightly less concise but easier to configure with variables to define the loop :
#!/bin/bash
for ((i=1;i<=5;i++))
do
echo $((i%2))
done
Another one, just for fun (would not do that in a read script, more like a little puzzle to figure out why it works). Remove the "false" line to begin with 0.
#!/bin/bash
false
for i in {1..5}
do
echo $? ; [[ $_ != 0 ]]
done
You can utilize the following trick using the brace expansion.
echo {,,,,}{1,0}
This one will generate 5 pairs of ones and zeroes.
I'm trying to write what would seem to be a simple if statement in most languages, however in bash this doesnt seem to work at all.
When I run the script it always enters the first if statement. Can anyone offer help me as to what I am doing wrong?
PERC=.5
if [ "$PERC" > "1.00" ]
then
echo "Entered first statement"
else
if [ "$PERC" < "1.00" ]
then
echo "Entered second statement"
fi
fi
Thanks for your help.
> and < compare strings, not numbers (and must be backslashed or quoted in single [...]). Use -gt, -lt etc. to compare numbers, or use arithmetic conditions:
if (( a < b || b <= c )) ; then
Note, however, that bash only handles integer arithmetics. To compare floats, you can use bc:
if [[ 1 == $( bc <<< '1.5 < 1.00' ) ]] ; then
> and < are the I/O redirection operators. So
if [ "$PERC" > "1.0" ]
is executing the command [ "$PERC ], redirecting the output to the file 1.0, and then if is testing whether the command succeeded. [ "$PERC" ] simply tests whether "$PERC" is a non-empty string.
To use them as operators in the test command, you need to quote or escape them:
if [ "$PERC" '>' "1.0" ]
You could also use bash's [[ conditional syntax instead of the [ command:
if [[ $PERC > "1.0" ]]
This question already has answers here:
"Command not found" when attempting integer equality in bash
(3 answers)
Closed 8 years ago.
Output 1:
Enter your value: 12
./testscript.sh: line 4: 12: command not found
Your value is more than 10
Output 2:
Enter your value: 5
./testscript.sh: line 4: 12: command not found
Your value is more than 10
I need to know what is wrong. My Linux test is just tomorrow:(
#!/bin/bash
echo -n "Enter your value: "
read value
if [$value -lt 10]
then
echo "Your value is less than 10"
else
echo "Your value is more than 10"
fi
your if statement should be as below. A space before ']' and space after '['
if [ $value -lt 10 ]
EDIT
As per the comments
you can always add optional ; at the end of the line. The below script would work fine.
a=20;
if [ $a -gt 10 ];
then
echo "true";
else
echo "false";
fi
you need to put your condition in the [ condition ]. The below one does not work.
( condition )
It would say command not found. As '(' is not command. where as '[' is a command in order to check a condition
And the more appropriate way is to use [[ ]] over [ ] when in Bash since you can avoid word splitting and pathname expansion with it. Other conditions can be added as well:
if [[ ! $value =~ [0-9]+ ]]; then
echo "Invalid input."
elif [[ value -lt 10 ]]; then
echo "Your value is less than 10."
elif [[ value -eq 10 ]]; then
echo "Your value is 10."
else
echo "Your value is more than 10."
fi
To throw yet another option into the mix: bash has (( ... )) -- arithmetic conditions:
if (( $value < 10 )); then ...
http://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs
One thing you can do in bash with arithmetic expressions is to drop the $.
if (( value < 10 )); then
This is documented a bit obscurely in Shell arithmetic: "Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax."
This may appeal to aficionados of C-like languages, but it's a bit out of step with the rest of the languages, and it doesn't apply to all variables (such as special paramaters ($#)
and array elements (${foo[3]})).
For the below script I am expecting the output to be msg_y and msg_z. But it is printing msg_x and msg_z. Can somebody explain to me what is going on?
#!/bin/bash
set -x
vr=2
echo $vr
if [ $vr > 5 ]
then
echo "entered 1st if"
echo "msg_x"
echo "out of 1st if"
if [ $vr < 8 ]; then
echo "in of 2nd if"
echo "msg_y"
else
echo "msg_z"
fi
else
if [ $vr > 1 ]; then echo "msg_y"
else echo "msg_z"
fi
fi
This expression
[ $vr > 5 ]
is being parsed as an output redirection; check to see if you have a file named "5" now. The output redirection is vacuously true. Note that the usual admonition to quote parameters inside a test expression would not help here (but it's still a good idea).
You can escape the > so that it is seen as an operator in the test command:
if [ "$vr" \> 5 ]; then
or you can use the integer comparison operator -gt
if [ "$vr" -gt 5 ]; then.
Since you are using bash, you can use the more robust
conditional expression
if [[ $vr > 5 ]]; then
or
if [[ $vr -gt 5 ]]; then
or use an arithmetic expression
if (( vr > 5 )); then
to do your comparisions (likewise for the others).
Note: although I showed how to make > work as a comparison operator even when surrounded by integers, don't do this. Most of the time, you won't get the results you want, since the arguments are compared lexicographically, not numerically. Try [ 2 \> 10 ] && echo What? Either use the correct integer comparison operators (-gt et al.) or use an arithmetic expression.
[root#jiaoyou ~]# test 1 = 1 -a 0 = 1
[root#jiaoyou ~]# if [1 = 1 -a 0 = 1]then echo 1;else echo 2;fi
-bash: syntax error near unexpected token `else'
Why test doesn't give any output and the if statement fails?
Can someone point out what's wrong here?
UPDATE
Can someone illustrate how to simulate complex expressions like 1=1 and (0=1 or 1=1) with [[?
Return codes are not printed; you must echo the value of $? in order to see it.
[ is a command. Just as you don't write echoHelloworld, you can't write [1 ....
There are several equivalent ways of doing the test you're looking for. First, essentially the way you're doing it, but with the syntax fixed (added required spaces around the parameters to the [ (aka test) command, and a ; between ] and then):
if [ 1 = 1 -a 0 = 1 ];then echo 1;else echo 2;fi
Here's a version with explicit grouping in the expression; note that the parentheses have to be escaped because they're special characters in the shell, but in this case we want them passed to the [ command as arguments:
if [ \( 1 = 1 \) -a \( 0 = 1 \) ];then echo 1;else echo 2;fi
Another version with explicit grouping, this time using two separate [ commands connected with bash's && operator (note that this can also connect other commands):
if [ 1 = 1 ] && [ 0 = 1 ];then echo 1;else echo 2;fi
And finally, a couple of examples using bash's [[ operator instead of the [ command; note that [[ isn't a command, so its expression isn't parsed as command arguments, allowing it a more intuitive syntax (e.g. allowing && and || as operators, and parentheses and !<> comparisons without escaping):
if [[ 1 = 1 && 0 = 1 ]];then echo 1;else echo 2;fi
if [[ ( 1 = 1 ) && ( 0 = 1 ) ]];then echo 1;else echo 2;fi
Some more notes/warnings: the = operator being used in all of these examples does string comparisons rather than numeric comparison (i.e. [ 1 = 01 ] is false); if you want numeric comparison use -eq instead (similarly, < and > are string comparisons, while -lt and -gt are numeric). Also, if you're comparing strings that include spaces, '[' can mistake the string for part of the expression under some circumstances (esp. if you don't have the strings wrapped in double-quotes); as far as I know [[ never has this problem. Overall, if you're using shells that support [[, it's much easier to deal with than [.
As #IgnacioVazquez-Abrams correctly states, test just sets the error code.
That said, here's a nice bash-ism for quickly checking the error value:
test [ expr ] && echo SUCCESS || echo FAIL
Due to how && and || short circuit, that will output SUCCESS or FAIL depending on whether test returns 0 or non-0.