Jump to content




ignoring if statement for else


  • You cannot reply to this topic
8 replies to this topic

#1 BlueZero

  • Members
  • 41 posts

Posted 13 June 2013 - 07:55 PM

For the most part this program works, but for some reason it's ignoring my first if statement for detecting the startup file and going to the else. I'm not sure if I set up the if statement up properly, but here's my code:

function newCheck()
shell.run("clear")
if fs.exists("startup") then
  local rf = fs.open("startup", "r")
  local ef = fs.open("startup", "a")
  lines = ""
  while lines do
   lines = rf.readLine()
   if lines and string.find(lines, [[shell.run("bluelock")]]) then --Completely ignoring this.
	rf.close()
	ef.close()
	return blueLock()  
   else
	print("Preparing for first time run.")
	sleep(2)
	print("Startup File Detected. Adding BlueLock to startup file.\n\nNote: If you have a custom sequence for your startup file BlueLock has been added to the end of the file.\n\nBe sure to place BlueLock's startup opperation in a more appropriate spot in your file.")
	ef.write([[shell.run("bluelock")]].."\n")
	ef.close()
	rf.close()
	sleep(3)
	return blueLock()
   end
  end
elseif not fs.exists("startup") then
  local rf = fs.open("startup", "r")
  local ef = fs.open("startup", "a")
  print("Preparing for first time run.")
  sleep(2)
  print("Startup File Not Detected, creating Startup File.")
  ef.write([[shell.run("bluelock")]])
  ef.close()
  sleep(3)
  return blueLock()
end
end

I don't know if there's anything else in this I missed, but so far that's what seems to be causing a problem.

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 13 June 2013 - 08:23 PM

Try not opening the same file in two different handles in two different modes at the same time. Open the read handle, then when you're done, close it. Then if you need the append handle, open it, write, and close it again. Don't have both open at once.

#3 BlueZero

  • Members
  • 41 posts

Posted 13 June 2013 - 08:33 PM

View PostLyqyd, on 13 June 2013 - 08:23 PM, said:

Try not opening the same file in two different handles in two different modes at the same time. Open the read handle, then when you're done, close it. Then if you need the append handle, open it, write, and close it again. Don't have both open at once.

Fixing that seemed to fix my file access problems in trying to delete the startup file, but it's still skipping over the first if and going to my else even if I've got "shell.run("bluelock")" within the startup file.

#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 13 June 2013 - 08:37 PM

Please post the updated code.

#5 BlueZero

  • Members
  • 41 posts

Posted 13 June 2013 - 08:42 PM

function newCheck()
shell.run("clear")
if fs.exists("startup") then
  local rf = fs.open("startup", "r")
  local ef = fs.open("startup", "a")
  lines = rf.readLine()
  rf.close()
  while lines do
   if lines and string.find(lines, [[shell.run("bluelock")]]) then
    return blueLock()   
   else
    print("Preparing for first time run.")
    sleep(1)
    print("\nStartup File Detected. Adding BlueLock to startup file.\n\nNote: If you have a custom sequence for your startup file BlueLock has been added to the end of the file.\n\nBe sure to place BlueLock's startup opperation in a more appropriate spot in your file.")
    ef.write([[shell.run("bluelock")]].."\n")
    ef.close()
    sleep(3)
    return blueLock()
   end
  end
elseif not fs.exists("startup") then
  local rf = fs.open("startup", "r")
  local ef = fs.open("startup", "a")
  print("Preparing for first time run.\n")
  sleep(1)
  print("Startup File Not Detected, creating Startup File.")
  ef.write([[shell.run("bluelock")]])
  ef.close()
  sleep(3)
  return blueLock()
end
end

I'm guessing this is what you meant. If not, I'm at a loss. Still rather new to the language.

#6 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 13 June 2013 - 10:16 PM

You're probably wanting to replace `lines = rf.readLine()` with `lines = rf.readAll()`.

#7 BlueZero

  • Members
  • 41 posts

Posted 13 June 2013 - 10:30 PM

View PostLyqyd, on 13 June 2013 - 10:16 PM, said:

You're probably wanting to replace `lines = rf.readLine()` with `lines = rf.readAll()`.

Changed that but still the same thing. What seems to happen in the startup file is regardless of shell.run("bluelock") being in there, it still adds to it. If I run it 3 times, there'll be 3 entries of the shell.run command. It's not identifying shell.run being in there, but it's at least identifying that startup exists.

Lol, this one's confusing the hell out of me.

#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 13 June 2013 - 10:45 PM

The problem is that string.find requires the use of Lua Patterns so it should be this
string.find(lines, [[shell.run%("bluelock"%)]])
notice the % before each ( ), this is because the brackets in Patterns have a special meaning. Info on Patterns.

EDIT: Also in the else when the startup doesn't exist you don't need this line
local rf = fs.open("startup", "r")
Also all of these
return blueLock()
won't work, I'm assuming in the bluelock file it creates a global function called blueLock, but after you create the startup you don't run either it or the bluelock program meaning that function doesn't exist...

Edited by theoriginalbit, 13 June 2013 - 10:48 PM.


#9 BlueZero

  • Members
  • 41 posts

Posted 13 June 2013 - 10:52 PM

View Posttheoriginalbit, on 13 June 2013 - 10:45 PM, said:

The problem is that string.find requires the use of Lua Patterns so it should be this
string.find(lines, [[shell.run%("bluelock"%)]])
notice the % before each ( ), this is because the brackets in Patterns have a special meaning. Info on Patterns.

EDIT: Also in the else when the startup doesn't exist you don't need this line
local rf = fs.open("startup", "r")
Also all of these
return blueLock()
won't work, I'm assuming in the bluelock file it creates a global function called blueLock, but after you create the startup you don't run either it or the bluelock program meaning that function doesn't exist...

Ah! That did the trick. My bad. I remember going over this bit before but I guess it didn't stick. Thanks man. My program's almost complete now.





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users