iPhone Shell, reading output - iphone

For executing a shell command I'm using system("ls"); command. Is there any way I can read the output?

You can't do this on the iPhone since there is no shell. On a standard POSIX system (like OS X on the desktop) you would either use popen, or set up a pipe between your app and the child process manually using fork, exec, and pipe.
If you actually want to list the contents of a directory, use NSFileManager.

Related

Perl - Directory Management on Different Operating Systems

I am new in Perl. I am using the following command to remove a folder in Perl, under Windows:
system "del trash_folder";
But I intend to run the same script under Unix as well. I could get the OS name in the code and run a different command based on the OS. But is there no better way in Perl? I am thinking of possibly an API or so that is OS-ignorant.
The del command is never going to create a new directory, is it? :-)
If you want to create directories, use the mkdir command.
If you want to remove directories, use the rmdir command.
Update: In general, if you have a choice between using a Perl built-in function or an external command, then the Perl function will be the better choice. Firstly, your code will be more portable and, secondly, opening a sub-shell to run the external command will slow your program down.

How to change working directory using Net::SSH2?

use Net::SSH2;
my $ssh2 = Net::SSH2->new();
$ssh2->connect($hostname);
$ssh2->auth_password($user,$pass);
$chan = $ssh2->channel();
$chan->exec("cd dir1");
$chan->exec("command file1.txt");
The above doesn't work and command cannot find dir1/file1.txt. How do you change the working directory using Net::SSH2?
According to the documentation, each invocation of $chan->exec() runs in its own process on the remote. The cd dir1 in the first exec affects only that execution. The next exec is a completely separate process.
The simplest way to solve the problem would be to pass the full path in the command, i.e.
$chan->exec("command dir1/file1.txt");
You could also try setting the PATH variable using $chan->setenv() but that probably will be prohibited by the remote side.
Note also (from the process section):
... it is also possible to launch a remote shell (using shell) and simulate the user interaction printing commands to its stdin stream and reading data back from its stdout and stderr. But this approach should be avoided if possible; talking to a shell is difficult and, in general, unreliable.

Source configuration filr in terminal though perl script

I need to source configuration file 'eg.conf' to terminal though perl script. I am using system command but its not working.
system('. /etc/eg.conf')
Basically I am writing script in which later point it will use the environment variable (under conf file) for execute other process.
It is not clear what you are trying to achieve, but if you want to make the config available from within Perl AND your config file is valid Perl code you can use do or require (see perldoc for more information).
What you are doing in your code is to spawn a shell with system, include the config inside this shell (which must be in shell syntax) and then exit the shell again which of course throws all the config away on close. I guess this is not what you intend to do, but your real intention is not clear.
What is your goal? Do you need to source eg.conf to set up further calculations from within a perl controlled shell, or are you trying to affect the parent shell that is running the perl script?
Your example call to system('. /etc/eg.conf') creates a new shell subprocess. /etc/eg.conf is sourced into that shell at which point the shell exits. Nothing is changed within the perl script nor in the parent process that spawned the perl script.
One can not modify the environment of a parent process from a child process, without the assistance of the parent process[1]. One generally returns code for the parent shell to source or to eval.
1: ok, one could theoretically affect the parent process by directly poking into its memory space. Don't do that.

implement bash command cd in perl

I tried to implement a bash command system("cd /home/user") in perl , but I get an error saying
Can't exec "cd": No such file or directory at temp.pl
Is there a way to change the current working directory to the specified one , and the change remains after the perl script has exited also.
No. A process can't change its parent process's current working directory. Shells implement commands like cd as "builtins", meaning they're a function in the shell itself, and not a separate process that gets run.
You can change the current directory in perl using chdir($dir), and that change will be inherited by child processes — but it won't be passed along to the parent process.
When you want to change the directory inside your Script, you can use the Perl command chdir('dir')
Example:
chdir($dir);
You actually cant modify the directory of the parents process, but you can of the current process
You cannot modify the current working directory of a different process in UNIX, at least not without some serious hackery.
This is why cd is a built-in in all shells. It is not an external program (nor can it be implemented as an external program).
cd is not a process, it is a shell builtin command that changes the current working directory for that shell process.
So use system("sh -c 'cd /my/dir'"). but here system command itself invoke another shall so still it not change directory.
use chdir for that.
cd is not a process, it is a shell builtin command that changes the current working directory for that shell process. So system("sh -c 'cd /cat/bat'") would "succeed", but still wouldn't change the working directory of your perl process; use chdir for that.

Using external commands in a perl console

I have written a script using perl comnsole Term::ReadLine::Gnu.
How can I run while I'm in the console external commands in the background?
I've managed to support various external commands such as ls -l etc. but I also wish to support running commands in the background such as emacs &, but I can't seem to be able to get a process to run in the background.
Is there a solution?
To execute something in the background, you can use system:
#!/usr/bin/env perl
use strict;
use warnings;
system q(emacs &);
print "hello";
This will open emacs in the background and print "hello" straight away. If you removed the & in the command it would wait for emacs to close before printing "hello".
As pointed out by #Brad in the comments, note that this will only work on systems where the shell understands the &. If you were running on Windows for example, you would have to change the system line to something like this:
system q(start /b program.exe);
See the system documentation for more details.