Solved

Does system("pause") work on Linux?

Posted on 2002-04-11
12
409 Views
Last Modified: 2010-04-21
Does system("pause") work on Linux?

int main(int argc, char* argv[])
{
 printf("hello world");
 system("pause");
 return 0;
}

On DOS, the above code would pause, and wait for a keyboard hit, before exiting the program.

Does Linux have a similar pause command?
0
Comment
Question by:Axter
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 4

Expert Comment

by:newmang
ID: 6935566
Axter

Why do you want to do it this way?

Surely all you need is the following

int main(int argc, char * argv[])
{

  printf("hellow world\n");
  getchar();
  return 0;
}

Cheers - Gavin
0
 
LVL 4

Expert Comment

by:newmang
ID: 6935570
Axter

Or the following to look more like DOS

int main(int argc, char * argv[])
{

 printf("hellow world\n");
 printf("Press any key to continue\n");
 getchar();
 return 0;
}

Cheers - Gavin
0
 
LVL 4

Expert Comment

by:newmang
ID: 6935576
Oops - ignore those last two comments - getchar doesn't return until newline is pressed - sorry.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:newmang
ID: 6935606
Oops - ignore those last two comments - getchar doesn't return until newline is pressed - sorry.
0
 
LVL 4

Expert Comment

by:newmang
ID: 6935731
OK - This works on my system.

Create a perl script

#!/usr/bin/perl
print "Press any key to continue\n";
system "stty cbreak </dev/tty >/dev/tty 2>&1";
$key = getc;
system "stty -cbreak </dev/tty >/dev/tty 2>&1";
print "\n";

Make this executable (chmod +x keypress.pl) and place it somewhere in the default path then call this in the system call in your C application.

Cheers - Gavin
0
 
LVL 4

Expert Comment

by:newmang
ID: 6935742
More....

#!/usr/bin/perl
print "Press any key to continue\n";
system "stty cbreak </dev/tty >/dev/tty 2>&1";
system "stty -echo </dev/tty >/dev/tty 2>&1";
$key = getc;
system "stty -cbreak </dev/tty >/dev/tty 2>&1";
system "stty echo </dev/tty >/dev/tty 2>&1";
print "\n";

now it doesn't echo the character the user enters.
You could call this script pause, place it in the default path and use it in scripts as well as programs.

Cheers - Gavin
0
 
LVL 30

Author Comment

by:Axter
ID: 6935922
newmang,
I'm not looking for a replacement for system("pause").

I just want to know if it works in Linux.  That's all.

I'm an expert in the C++ topic area, and I post a lot of example code.
In VC++ it's easier to put system("pause") at the end of the main() function, so that the program doesn't end before being able to see the result of your test code.

I looking to see how portable is this method.

I don't have a Linux machine, so I can't test it myself.

Does Linux have a pause command?
0
 
LVL 4

Accepted Solution

by:
newmang earned 25 total points
ID: 6935944
NO
0
 
LVL 2

Expert Comment

by:fremsley
ID: 6937088
> so that the program doesn't end before being able to see
> the result of your test code.

You do not need to pause under Linux because in a terminal
window and on the text console you can always scroll back
using the Shift-PgUp/PgDn keys. To keep your code portable
you can test for predefined macros like:

#ifndef __unix__
  system("pause");
#endif

Hope it helps
0
 
LVL 30

Author Comment

by:Axter
ID: 6937113
fremsley,
Thanks for the info, but I was just looking for a yes or no answer, and I'm already familiar with compiler directive methods.
0
 

Expert Comment

by:Synthesist
ID: 8771646

Hello Axter,

I believe what your looking for is in the "wait" command. A simple "man wait" should fill in the details for you. ;-)

This may be a bit late but I was looking for that same command again. Hate it when my scripts exit before the other processes are done.
0
 
LVL 30

Author Comment

by:Axter
ID: 8772236
Synthesist,
Thanks
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Optimize bash script 20 484
Installing rpm without root access 1 72
null coming in rpm name 3 82
Linux Ubuntu - Gave up waiting for root device. ALERT! /dev/sda1 does not exist 16 309
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question