How to test if command line argument is entered? - applescript

For my script, I'm getting an argument from user input on the command line, and I want to make sure the user doesn't forget to input the argument. So basically, I want to make my script do something like this:
on run argv
if (item 1 of argv exists) then
return "defined"
else
return "undefined"
end if
end run
Right now the part that says (item 1 of argv exists) does not work, but I hope someone can help me out. Thanks!

on run argv
if (count of argv) > 0 then
return "defined"
else
return "undefined"
end if
end run

Related

"Unexpected_end, expecting keyword_end"

I have an issue in one of my functions in my code. I am new to Ruby, so I am unsure of where my syntax error is. My irb is giving me a syntax error related to my end keywords, but I believe the syntax is correct
def function1
print "function 1 \n"
print "Please type 4 lines \n"
i = 0
fptr = (File.new("myFile.txt", "w"))
while i < 4
line = gets
fptr.write(line "\n")
i++
end
fptr.close()
end
This function should print two output lines, open a txt file, take in 4 lines of user input, and write them to the said file.
The problem is that i++ is not valid Ruby. Use i += 1 instead.

Recursive Function return value in vb script

Please see below function fnWaitCheckFinalStatus if Else part is executed in below code then value return by Function fnWaitCheckFinalStatus is coming blank because function this is called recursively fnWaitCheckFinalStatus.
Is there way to get return value of fnWaitCheckFinalStatus After exit function function should exit all its state.
How can I make it possible , any pointers on this.
Function fnWaitCheckFinalStatus(objStatusBar)
Dim blnRetValue : blnRetValue = True
Dim i : i=0
If objStatusBar.Exist Then
strValue=ObjStatusBar.GetROProperty("text")
Do
wait 10
strValue=ObjStatusBar.GetROProperty("text")
Loop While strValue = "Task Started"
End If
strValue1=ObjStatusBar.GetROProperty("text")
If strValue1="Task executed successfully" Then
blnRetValue1=True
fnWaitCheckFinalStatus = blnRetValue1
Exit Function
ElseIf strValue1="Task execution failed" Then
blnRetValue1=False
fnWaitCheckFinalStatus = blnRetValue1
Exit Function
Else
Call fnWaitCheckFinalStatus(objStatusBar)
End If
End Function
Consider "pass-through"ing the function result if you return from the recursion, like in this code (note the line with the !!! comment):
Function fnWaitCheckFinalStatus(objStatusBar)
Dim i : i=0
If objStatusBar.Exist Then
strValue=ObjStatusBar.GetROProperty("text")
Do
wait 10
strValue=ObjStatusBar.GetROProperty("text")
Loop While strValue = "Task Started"
End If
strValue1=ObjStatusBar.GetROProperty("text")
If strValue1="Task executed successfully" Then
fnWaitCheckFinalStatus = true
ElseIf strValue1="Task execution failed" Then
fnWaitCheckFinalStatus = false
Else
fnWaitCheckFinalStatus=fnWaitCheckFinalStatus(objStatusBar) ' !!!
End If
End Function
Also, I eliminated the result buffer variable. You donĀ“t need it, so you can scratch it.
Also, I'd avoid exit function in this case to keep the code simpler (one entry point, one exit point), so I eliminated that, too.
Generally speaking, there is no obvious reason for using recursion here since you pass exactly the same argument as you receive, so the recursive call will do exactly the same as its caller scope. Use a loop instead.

How to get the ruby.exe Return Code in CMD Bat?

I'm trying to get the return value of a ruby script in cmd.exe. The ruby script will return -1 if exception is caught and 0 if success.
rescue Exception => ex
puts ex.message
returnvalue = -1
else
returnvalue = 0
ensure
puts returnvalue
in the cmd batch, calling the script using ruby.exe
ruby tt.rb 1 %1
But I'm not able to retreive the returnvalue in the cmd batch file (e.g. using %errorlevel%). Is there any way to do this?
thanks!
puts -1 does not make ruby.exe return -1; it makes it print -1. It returns -1 if you do exit -1.
In *nix, it is easy to capture the output (as opposed to the exit code) of the program into a variable. Under Windows, not so easy; there is a trick you can do involving the for construct, but it is of limited use.

Print the value of a local variable through GDB

while debugging I need to print the value of a variable that is declared in the else block. something like this :
if(condition){
}
else {
string str = "abcd";
strcpy(globalvariable,str,sizeOf(str));
}
I want to see the value of str.
Run the program inside the debugger.
Set break point to stop the execution of program sequence using break command. In your case, (gdb) break strcpy to break every time it is being called strcpy in else.
To print you can use any of the following, x str, x/s str, print str, print "%s", str.
You can't see the value of str if condition is true during program flow because it does not exist in memory in this case.
You have to enter else block somehow, either during normal program flow or using gdb jump command.

C - passing an unknown command into execvp()

I'm writing a fake shell, where I create a child process and then call execvp(). In the normal shell, when I enter an unknown command such as 'hello' it returns 'hello: Command not found.' However, when I pass hello into execvp(), it doesn't return any error by default and just continues running the rest of my program like nothing happened. What's the easiest way to find out if nothing was actually run? here's my code:
if(fork() == 0)
{
execvp(cmd, args);
}
else
{
int status = 0;
int corpse = wait(&status);
printf(Child %d exited with a status of %d\n", corpse, status);
}
I know that if corpse < 0, then it's an unknown command, but there are other conditions in my code not listed where I don't want to wait (such as if & is entered at the end of a command). Any suggestions?
All of the exec methods can return -1 if there was an error (errno is set appropriately). You aren't checking the result of execvp so if it fails, the rest of your program will continue executing. You could have something like this to prevent the rest of your program from executing:
if (execvp(cmd, args) == -1)
exit(EXIT_FAILURE);
You also want to check the result of fork() for <0.
You have two independent concerns.
1) is the return value of execvp. It shouldn't return. If it does there is a problem. Here's what I get execvp'ing a bad command. You don't want to wait if execvp fails. Always check the return values.
int res = execvp(argv[1], argv);
printf ("res is %i %s\n", res, strerror(errno));
// => res is -1 No such file or directory
2) The other concern is background processes and such. That's the job of a shell and you're going to need to figure out when your program should wait immediately and when you want to save the pid from fork and wait on it later.

Resources