I'm trying to process some files in increments of 50. It seems to work, but I'm getting an error that the command isn't found.
File sleepTest.sh:
#!/bash/bin
id=100
for i in {1..5}; do
$((id+=50))
sh argTest.sh "$id"
sleep 2
done
File argTest.sh:
#/bash/bin
id=$1
echo "processing $id..."
The output is
sleepTest.sh: line 6: 150: command not found
processing 150
sleepTest.sh: line 6: 200: command not found
processing 200
sleepTest.sh: line 6: 250: command not found
processing 250
sleepTest.sh: line 6: 300: command not found
processing 300
sleepTest.sh: line 6: 350: command not found
processing 350
So it clearly has an issue with how I'm incrementing $id, but it is still doing it. Why? And how can I increment $id. I tried simply $id+=50, but that did not work at all.
Leave out the $.
((id+=50))
((...)) performs arithmetic. $((...)) performs arithmetic and captures the result as a string. That would be fine if you did echo $((...)), but if you write just $((...)) then the shell treats that number as the name of a command to execute.
var=$((21 + 21)) # var=42
echo $((21 + 21)) # echo 42
$((21 + 21)) # execute the command `42`
Such assignments are legal inside arithmetic expressions. However, bash still tries to interpret the result of the expression as the name of a command. Either pass it as an argument to the : command (the POSIX way)
: $((id+=50))
or use a bash arithmetic statement instead of an arithmetic expression
((id+=50))
Related
Having string of pattern:st2-api-alpha
wanted to trim them like st2-api
Basically wanted to remove the text followed by 2nd occurence on '-' for each string.
Below are my statements:
for (( i=1; i<${#all_containers_list[#]}; i++ )){
echo "trimmed[$i] - "${all_containers_list[i]}|cut -f1-3 -d'-'
temp="$(${all_containers_list[i]}| cut -f1-3 -d'-')"
echo "temp is:$temp"
all_containers_list[i]=$temp
echo "all_containers_list[$i] is ${all_containers_list[i]}"
}
when i echo:
trimmed[1] - st2-ui
/home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line
139: st2-ui-server-alpha: command not found
temp is:
all_containers_list[1] is
trimmed[2] - st2-api
/home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line
139: st2-api-alpha: command not found
temp is:
all_containers_list[2] is
trimmed[3] - st2-whiteboard
/home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line
139: st2-whiteboard-alpha: command not found
temp is:
all_containers_list[3] is
trimmed[4] - st2-aspose
/home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line
139: st2-aspose-alpha: command not found
temp is:
all_containers_list[4] is
trimmed[5] - determined_bassi
/home/****/st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line
139: determined_bassi: command not found
temp is:
all_containers_list[5] is
trimmed[6] - st2-ui
/home//st2-monitoring/monitors/getAllContainerStatus_v1.1.sh: line
139: st2-ui--alpha: command not found
temp is:
all_containers_list[6] is
echo "trimmed[$i] - "${all_containers_list[i]}|cut -f1-3 -d'-'
--- This is working fine.
But the below assignment is not working
temp=$(${all_containers_list[i]}| cut -f1-3 -d'-')
Request your help to fix the same!
OS is CentOS , bash script
I think calling subprocesses just for removing a part of a string, is a bit of an overkill. Assuming that you have stored your string in a variable, say
str=st2-api-alpha
you could simply do a
if [[ $str =~ [^-]*-[^-]* ]]
then
str=${BASH_REMATCH[0]}
fi
If the string does not contain a dash, it is left unchanged. You can trvially generalize this solution to your case of having an array of strings.
When I am executing the below script, I am getting the following error :-
The script executes infintely and below line is printed everytime.
"line 9: 1=1+2: command not found". Why?
#!/bin/bash
echo "Script 1 - Linux Scripting Book"
x=1
while [ $x -le 45 ]
do
echo x : $x
$x=$x+2
done
echo "End Of Script 1"
exit 0
Also if I change the $x=$x+2 to x+$x+2 then also I am getting the below error.
line 6: [: 1+2: integer expression expected
Same script when executed like this runs fine.
#!/bin/bash
echo "Script 1 - Linux Scripting Book"
x=1
while [ $x -le 45 ]
do
echo x : $x
let x=x+2
done
echo "End Of Script 1"
exit 0
You get line 9: 1=1+2: command not found because 1=1+2 is what $x=$x+2 is expanded into.
Use expr or let or ((...)) for integer calculations and bc for floating point:
let x=x+2
((x=x+2)) #same as above
((x+=2)) #same
((x++)) #if adding just one
((++x)) #if adding just one
x=$((x+2))
x=`expr $x + 2` #space before and after +
x=$(echo $x+2|bc) #using bc
x=$(echo $x+2.1|bc) #bc also works with floating points (numbers with decimals)
Since this part of the question isn't cleared yet, and not fine to post in a comment, I add this partial answer:
x=1; for i in 1 2 3 ; do x=$x+2; echo $x; done
1+2
1+2+2
1+2+2+2
As a side note: Don't use exit 0 at the end of your script without a good reason. When the script is done, it exits by itself without your help. The exit status will be the exit status of the last command performed, in your case a simple echo, which will almost always succeed. In the rare cases it fails, you will probably without intention hide that failure.
If you source the script, the exit will throw you out of your running shell.
But you can rewrite your while loop like this:
x=0
while (($((x)) < 9))
do
echo x : $x
x=$x+2
done
echo $((x))
x : 0
x : 0+2
x : 0+2+2
x : 0+2+2+2
x : 0+2+2+2+2
10
Because that's not the Bourne shell syntax for setting a variable; it looks more like Perl or PHP. The $ is used for parameter expansion and is not part of the variable name. Variable assignment simply uses =, and let evaluates arithmetic expressions (much like $((expression))). Another syntax that should work is x=$((x+2)). Note that these arithmetic evaluations are a bash feature; standard unix shells might require use of external tools such as expr.
I'm trying to batch modify some images using a bash script, and to print out the progress. It looks to me like bash is interpreting the increment to counter as a command and is giving the following error:
augment_data.sh: line 20: 0: command not found
Here is the code:
for file in *.jpg
do
convert $file -rotate 90 rotated_"$file"
((counter++))
if $((counter % 10 == 0)); then
echo "Rotated $counter files out of $num_files"
fi
done
with line 20 being the one with the counter increment operation.
How can I fix this so that I don't get the error message?
In an arithmetic substitution, the result of an arithmetic operation is substituted in the position of the operation itself.
In this case, $(( 1 == 0 )) has an arithmetic result of 0, and $(( 1 == 1 )) has a result of 1.
Thus, if you use $(( ... )), then this 0 or 1 is substituted in that position, and so gets run as a command. Since you don't have commands named 0 or 1 (probably), either of these will result in a command not found error.
If you use (( ... )), then the arithmetic result directly sets return value, but no expansion takes place.
I have a problem with the done.
It says I have some typo error but I can't figure what's wrong at all.
Here is the code:
#./bin/bash
until [$err == 0];
do
java -Xms512m -Xmx512m -cp lib/*:lib/uMad/*:mysql-connector-java-5.1.15-bin.jar:l2jfrozen-core.jar com.l2jfrozen.gameserver.GameServer
err=$?
sleep 5
done
Your shebang line is wrong. #./bin/bash will not execute bash.
It should read #!/bin/bash. You are probably using a shell other than bash to invoke this script.
Also, beware that the [$err == 0] line expands the value of $err, which is probably an empty string, unless it has been exported. If it's empty, this will result in an error, because Bash will be interpreting [ == 0].
The safest approach is this:
unset err
until [ "$err" == "0" ];
do
# etc...
done
From my experience when working with brackets and if loops, you need proper spacing and double, not single brackets. There needs to be space on each side of the double brackets with the exception of the semi-colon. Here is an example block:
#!/bin/bash
err=5
until [[ $err == 0 ]]; do
((err-=1));
echo -e "$err\n";
sleep 3
done
I do not see why the same would not apply to a do until loop.
You're probably aware but your heading has a period in it instead of a shebang.
#./bin/bash
Hi I want to write and empty body loop. I just want the loop counter to increment so I want the cpu to stay busy without any IO operation. Here is what I have written but it gives me an error:
#!/bin/bash
for (( i = 0 ; i <= 1000000; i++ ))
do
done
root#ubuntu:~# ./forLoop
./forLoop: line 4: syntax error near unexpected token `done'
./forLoop: line 4: `done'
You must specify at least one command in a loop body.
The best command for such a purposes is a colon :, commonly used as a no-op shell command.
You could put a no op command inside the loop like true or false (do nothing successfully or unsuccessfully respectively).
This will be a tight loop and will burn CPU. Unless you want to warm up your computer on a cold morning, you can simply say i=1000000 and have the same effect as the loop.
What is it that you're trying to achieve?
#!/bin/bash
let i=0
while [[ $i -le 1000000 ]]; do
let i++
done
You could use sleep x if you want to delay for x number of seconds.