loop not working in bash
Hi people,
I have a small bash script in which i use "while" or "for" loop and none of them are working. This below is the part that's giving me head aches: Code:
if [ "$1" == "z" ] && [ ! -z "$2" ] ; then and break and of course remember the last $no. when searching for file.$no it loops from 1 to 100 and the first file it finds is file.8 so it should return no=8 why is it returning no=1 ?? i tried for loop like this: Code:
if [ "$1" == "z" ] && [ ! -z "$2" ] ; then Can someone please help me with this? |
First things first. Please use code tag so the code is more readable.
I doubt that the first file it finds is file.8. Lets assume the if clause is true and we have our variables no and end set. We then go to the while and loop for ever. Our exit from the while loop is the break if file.$no is not found. Do you have a file named "file.1"? I doubt it. Because if oyu would the while loop would be endless. But why. Probably cause you dont increment $no, no? I did not look at the second move. Specially cause you are looking for ips and not file. Use some set -x at the start of your script to see what bash actually does. There are some more bash options to debug things. You might also use some echo commands to see what the content of various variables are. |
That is what the script should do... find "file.8" because file.1 to file.7 don't exist, and then break with last $no. How can i do this?
What am i doing wrong man ? i tried everything. |
I did everything by the book, googled for every statement and copied what experts wrote. if you can't write the solution at least give me some hints.
|
Here is a hint.
Quote:
https://bash.cyberciti.biz/guide/Logical_Not_! |
Code:
if [ "$1" == "z" ] && [ ! -z "$2" ] Code:
for (( $no; $no<$end; $no++ )) Code:
if [ ! -f "file.$no" ] If file exists then ... what is wrong? |
Okay,
tried it like this Code:
if [ "$1" == "z" ] && [ ! -z "$2" ] ; then Code:
line 13: ((: 1++ : syntax error: operand expected (error token is "+ ") Code:
for (( $no; $no<$end; $no++ )) ; do Code:
if [ "$1" == "z" ] && [ ! -z "$2" ] ; then |
Code:
[ ! -z "$2" ] and Code:
[ ! -f "file.$no" ] they are written the same with "!" |
Code:
if [ condition ]; then The [ ! -z $2 ] means true if the second command line argument length is not zero. http://www.tldp.org/LDP/Bash-Beginne...ect_07_01.html |
check this out:
http://stackoverflow.com/questions/6...h-shell-script Code:
if [ -z "$1" ] |
Code:
no=1; end=100 |
So if i understand correctly, you want to find the first file that does exist, ie. file.8 which would then have a value of 8 assigned to 'no'?
I would tackle the logic a little differently: Code:
if [[ "$1" == "z" && -n "$2" ]] ; then |
i ran this:
Code:
if [[ "$1" == "z" && -n "$2" ]] ; then Code:
[[ -f "file.$no" ]] && break |
The point is to teach the OP about conditionals and not spoon feed the answer.
Code:
if [ "$1" == "z" ] && [ ! -z "$2" ]; then |
this is the same as my last reply
Code:
if [ "$1" == "z" ] && [ ! -z "$2" ] ; then Code:
[[ -f "file.$no" ]] && break |
All times are GMT -5. The time now is 06:04 AM. |