Programming :: Return And Exit In C
Jun 3, 2011What are the uses of return 0, return 1 and exit statements in C
View 4 RepliesWhat are the uses of return 0, return 1 and exit statements in C
View 4 RepliesI have this command in ksh:
1. typeset -i INTEGER INTEGER=aaa In all platforms except of Linux it is failing and return exit code 1 Why in Linux it is not failing? I must it for my number tests
2. The same for printf command. Only in Linux the bellow command is not failing: printf "%d" aaa
I learn C++ reading the <C++ primer 4th>, question1.2 let me test if return -1 how the compiler handle this exit code.
and in gcc (GCC) 4.1.1 20070105 (Red Hat 4.1.1-52), I get this show:
[youxi600@Arthas C++ Primer]$ cat q1_2.cpp
int main()
{
return -1;
}
[youxi600@Arthas C++ Primer]$ gcc q1_2.cpp -o q1_2
[youxi600@Arthas C++ Primer]$ ./q1_2
[youxi600@Arthas C++ Primer]$ echo $?
255
[youxi600@Arthas C++ Primer]$
anyone can give me an advise? Many thanks!
I noticed that if I have "exit" in a bash script file., e.g. script.sh,that when the word "exit" is reached, and the script file being executed is not in the PATH nvironment, i.e. ". script.sh", the whole konsole shell profile is exited! What gives here? Is there another command compatible to "exit" to prevent this, or will I just have the leave the "." part in the PATH enviroment, which is, to my understanding, is not recommended? I desire for a "goto" function in bash script files
View 11 Replies View RelatedIn linux, creating thread is same as process (clone()), except the virtual address space gets shared with the parent.If a running main process(thread) creates new thread, and if main thread exits, why should the new thread too exit? both are different entities, The same doesn't happen if the child thread exits, the parent thread would be alive.
View 1 Replies View RelatedI trying to write a UART(interfacing of serial devices) to linux machine but after I execute the following code to receive data I need to enter key (carriage return).... but I don't want to remove carriage return/enter key
[Code]....
i am having two small issues with a function i have made.sorry if it is a mess, i am still learning bash.the first is calling the nonpersistssh function (second line) and assigning the return value to nonpersistdiag.the function returns 1, but nonpersistdiag seems to only contain 0. i am unsure on how to proceed.the second problem is the nested else clause on line 10. it is a syntactical error. how would i declare it correctly?
Code: function endsession(){
nonpersistdiag=$[nonpersistssh]# a function that returns an exit code
sudo /etc/init.d/ssh stop; sshdiag=$?
[code]....
On this link [URL]4 a return type is defined
Code:
return ((unsigned int)(unsigned long)base & TBASE_DEFERRABLE_FLAG);
What is the above function returning.I am not clear with definition of what is being returned in the above code.
I'm confused about what "echo $0" in a shell actually return. Consider these tests:
Code:
[root@e11apvl151 ~]# # Login shell:
[root@e11apvl151 ~]# echo $0
-bash
[root@e11apvl151 ~]# # Starting a new shell
[root@e11apvl151 ~]# bash
[root@e11apvl151 ~]# echo $0
bash
[root@e11apvl151 ~]# screen
[root@e11apvl151 ~]# echo $0
/bin/bash
Depending on wether the shell is a login shell, a regular shell, or a screen utility, I get different results.
I have a file:
979798707
787862348
766428634
I want to see if all the records in the file are present in the contents of the files of a particular directory.
Basically I want to say if grep doesn't return anything, then report.
For example in /tmp dir I have 4 files and flast 2 values (787862348 and 766428634) are present in the files of /tmp dir, but first one (979798707) is not. I want to echo that in a reporting file.
something like:
while read line
do
# if ! grep -rl $line /tmp
echo $line >> are_not_present
done < "myFile"
How do I achieve " if ! grep -rl $line /tmp"? That is, if the line is found by grep, then grep will print the output, but if grep does'nt find it, it will print nothing. How can I check if grep didn't find it (i.e. printed nothing)?
I'm reading a text file with fscanf using a loop until feof(inFile). How can I return to the top of the file? As in I have one loop that scans until the eof and then after it there's another loop and I want to start from the beginning of the file again scanning to the end of it. How do I get back there?
View 3 Replies View RelatedI am testing the serial ports on a Single Board Computer(SBC) running Linux kernel 2.6.29. I usually do this by connecting the serial port to another PC serial port, then doing "cat /dev/ttyS0" on PC and "echo hello > /dev/ttyS0" on the SBC. However in the current system, "echo hello > /dev/ttyS0" command does not return at all! Also no characters appear on the destination port. I am running the echo command as root. The system boot messages show that the serial port in indeed /dev/ttyS0.
View 2 Replies View RelatedI think my title pretty much explains it. I am writing a script and I want to start a program in the background, and when that program finishes I want to check the return value to make sure there was no error.For example normal I would do something like this:
#!/bin/sh
program
if [ ! $? -eq 0 ]; then
echo "There was an error"
exit 1
fi
Now I want to do something like this:
#!/bin/sh
PRTN=`program1 &`
program2
if [ ! $? -eq 0 ]; then
[code]....
In this case if program2 finishes before program1, I don't think the return value from program1 $PRTN would be valid at the time it is checked.
I am using pthread_create system call to create a thread. However pthread_create does not return the PID ( process ID). Is there any quick way to fınd the PID of the created thread.
View 1 Replies View RelatedC++ code:
Code:
#include<fstream.h>
[code]....
I write a little script by bash.
#!/bin/bash
a=`gawk '/Iterations /{print $0}' hoge.log | gawk 'BEGIN{FS=" "}{print ($4)}'`
a=`expr 3 '*' $a`
[code]...
I have always encountered this problem in ubuntu bash shell scripts that echo command in a function will be treated as a return value when used in a function. e.g.
[code]...
The output would simply be xyz. Hence the echo seems to function as a "return" command when used in a function with a return value.
$val = mkdir $directory, 755
print $val;
does't create directory but return as value '0'
[code]....
I have confusion regarding pthreads return value. All in short, is the is a valid return value ?
[source]
void * myThrRoutine(void *arg)
{
struct myStruct **myst = (struct myStruct**) arg;
cout<<"In thread "<<(*myst)->var<<(*myst)->msg<<endl;
int x = 20;
pthread_exit((void*)x);
}
[/source]
This seems to work fine on pthread_join , but I want to ensure it is not by chance.
But it's been hell finding an answer, or I just don't know what to look for..I have a prompt that asks for a float, and if the user doesn't put in a valid number, then it should die with an error message.
Code:
def die_with_error():
print 'ERROR: You didn't specify a valid number!'
[code]....
I am running a command pretty similar to the one below:
Code:
I need to capture the exit status of that part of the command.
What I am trying to do is set a variable that can be modified within my_job.sh, and used to exit.
However, no matter what I set "status" to in my_job.sh, it exits with "0".
What can I do differently?
How exactly can I use the exit status of a command in an if statement ?
Something like this:
Code:
I am using a program that reads in data from a serial port and then sends that data out over a TCP connection. The problem I'm having is that the only way I know to exit the program is to do a 'kill PID', but doing this means the program doesn't go through the motions of closing the TCP connection properly so I have to wait some random period of time for the port to free itself or else when I try to start it back up it tells me that it can't bind to the specified port.The general structure of the program is as follows
Code:
int main(){
// initialize some stuff
[code]...
I am trying to write a python script which logs into a NetApp filer and fetches some data. I am using the telnetlib library. Filer does not take exit command. So it expects Control-D. How to send that via python script?
View 5 Replies View RelatedI have an a script in /etc/rc.d/init.d script that I would like to start and stop my sample application. I have named my script "foo", then I can start running my application my typing $ foo start but this does not return [OK] like other scripts do and does not return to shell prompt. My application is basically like this
int main()
{
do {
printf("Hi
");
sleep(1);
} while (1);
}
What is missing from my application that prevents from returning the shell prompt?
I'm getting some information about C language and this session of a C book (follow the above link) is using a bad example for me. When I'm trying this example of function returning a pointer, my compiler is stating a warning that I return a pointer to a local variable. I realized that it is error prone after all this variable may be override before the function has done his execution. And the author is fooling me saying that this example is "perfectly safe". I'm wrong? There is something that I don't got yet? Sorry but this site is preventing me to post the link of book cause I'm a newbie, so a need the hack it. Just strip out the question signs:
{LINK}
?w?w?w
?cs.cf.ac.uk?
[code]....
I have functions which return different data like: int, char*, double...
I also have a list of datatypes (INTEGER, TEXT, REAL...) which can be returned.
I need to map a datatype with function, which purpose is to return it. Therefore, when I determine a datatype, I would like to call the required function without doing switch all the time.
My idea is to have an array of structs like this:
Code:
struct type_func_map
{
int type;
void (*func_wrapper_int)(void); //void (*func_wrapper_text)(void); void (*func_wrapper_real)(void);
};
[Code].....
I have a script that reads part of a line, delimited between the first and second intended part by a colon. Then it "chops" the part after the colon, which are words offset by commas (counting them beforehand so as to catch every word in the string's second part), like this:
Code:
"COLORS.JPG:red,orange,yellow,green,"
(Returning)
red
[code]....
single script that parses/breaks both parts of a line like this "COLORS.JPG:red,orange,yellow,green;blue,indigo,violet," so that the two parts, separated into single words (or two and three words, sometimes with spaces) can be used as single-line annotations and written to JPEG files using Exiv2. So far, I haven't been able to come up with a script that does this without one part of the total string(usually that part after the colon) becoming the first word in the second array. In other words, I look for this:
KEYWORDS:
[ ]red
[ ]orange
[ ]yellow
[code]....
Or vice-versa (ie, the second array winds up as a single-line "member" of the first). I think it's because I'm using a single while read loop to read the text file in which the filenames and substrings happen to be. If there's some way of reading a file once and going back to the beginning to read it again in another while loop, I haven't found it.
I have a function that returns a boolean value, True of False. Is there any convention whether 0 = False or 0 = True? It should be obvious, 0=False, 1=True. However programs usually return 0 on success and reserve higher numbers for error values. I.e. if the program finishes successfully (function int main() finishes successfully ), it returns 0 - and that contradicts that 0 should be false.
And especially if I want a function to do some computations on parameters passed by reference and return success status. Should it return 1 (true) for success, or should it return 0 for success - like any Unix program does?
Is there any convention for this?
I'm witting a piece of code that needs to read key presses. However I cannot use them as stream, but rather need discrete reads. The code is to simulate an io board interface. So When I press say "d" I need to read it in instantly, not wait for return. I've tried sdl, but no luck (I'm reading in a thread and it causes some problems) I'm using linux so can't use conio.h functions, I've tired gatch from cureses but this still waits for return.So in C/c++ is there a way I can read instant key presses.
View 1 Replies View Related