I've used the system function in C to call bash commands before, but wanted to learn this way too. The solution in the book worked perfectly. However, I tried changing the simple "ls -l /" they used to this one-liner:
If you're using a red hat based system instead of debian, you'll probably have to change the $10 to $9 in the above.
However, I'm getting the following errors:
Code:
# ./cbash2
Done with main program.
pyro# lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
Output information may be incomplete.
lsof: status error on 2>&1: No such file or directory
lsof: status error on |: No such file or directory
lsof: status error on grep: No such file or directory
lsof: status error on log: No such file or directory
lsof: status error on |: No such file or directory
lsof: status error on awk: No such file or directory
lsof: status error on '/openbox: No such file or directory
lsof: status error on { print $10 }': No such file or directory
lsof: status error on |: No such file or directory
lsof: status error on sed: No such file or directory
lsof: status error on '1d': No such file or directory
lsof 4.86
latest revision: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/
latest FAQ: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/FAQ
latest man page: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/lsof_man
usage: [-?abhKlnNoOPRtUvVX] [+|-c c] [+|-d s] [+D D] [+|-f[gG]] [+|-e s]
[-F [f]] [-g [s]] [-i [i]] [+|-L [l]] [+m [m]] [+|-M] [-o [o]] [-p s]
[+|-r [t]] [-s [p:s]] [-S [t]] [-T [t]] [-u s] [+|-w] [-x [fl]] [--] [names]
Here is the code I'm working with now:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
// Spawn a child process for the bash part later:
int spawn(char* program, char** arg_list)
{
pid_t child_pid;
// Duplicate this process:
child_pid = fork();
if (child_pid !=0)
// This is the parent process.
return child_pid;
else {
// Execute "program" which will call bash:
execvp (program, arg_list);
// execvp will only return if an error occurs
fprintf (stderr, "an error occurred in execvp\n");
abort();
}
}
int main()
{
//Argument list to pass bash commands:
char* arg_list[] = {
"lsof", // argv[0], the name of the program.
"2>&1",
"|",
"grep",
"log",
"|",
"awk",
"'/openbox/",
"{ print $10 }'",
"|",
"sed",
"'1d'",
NULL // Arg list must end with a null.
};
// Spawn the child process running bash.
spawn("lsof", arg_list);
printf("Done with main program.\n");
return 0;
}
I have a function that returns a bunch of exit codes, say func1, and in my code I'm trying to execute that function in an if statement. This is the closest I could get.
f1call=`func1 $arg1 $arg2`
if ]; then
...
fi
When I run the script the function never gets called. What's the right way... (7 Replies)
Hi ,
I m actually trying to implement pipes program,but after executing the execvp(),my program is getting hanged up :mad:
Actaully i m getting the desired output expected from execvp()...but once results are displayed on the output screen ,program is getting hanged up
values of... (3 Replies)
In a BASH library I'm creating, I have two functions that look like:
function check_process {
PIDFILE=$1
if ; then
PID=`cat $PIDFILE`
if && ; then
return 1
fi;
fi;
return 0
}
function fork_process {
CMD=$1
PIDFILE=$2
... (2 Replies)
I want to log into a remote server transfer over a new config and then backup the existing config, replace with the new config.
I am not sure if I can do this with BASH scripting.
I have set up password less login by adding my public key to authorized_keys file, it works.
I am a little... (1 Reply)
Hi Folks,
I have a small doubt, the binary commands under /bin and /sbin as well as other path binary files, if you peek deep into that, you can find the difference in the way of normal perl programming and some commands will be like binary files. how are the commands executing like the... (3 Replies)
Hi,
I want to run these two commands one after the other.
awk 'BEGIN {OFS="\t"} {print $2}'
sort -u
rather than typing awk 'BEGIN {OFS="\t"} {print $2}' file1 > file2, then sort -u file2 > file3. Is it possible to run both commands on file1 then get output file3?
Its kinda hard for... (5 Replies)
Hi, I do the following:
i) malloc some parameter structures
ii) fork
iii) in the child, I call execvp using these parameters
obviously, here I can never free() the structures as execvp should not return
iiii) I am currently doing waitpid in parent then freeing, but this does not seem to work... (1 Reply)
I have a script that writes another script with
cat >/usr/local/bin/myscript.sh <<EOF
#!/bin/sh
VAR=`run a command here`
EOF
Problem is, after this is run, I get:
$ cat /usr/local/bin/myscript.sh
#!/bin/sh
VAR=result of command
How do I stop that from happening with Macs... (2 Replies)
Ksh is my default shell, but I want use the bash shell since its convenient to me.
When I type a long command line in a terminal, it does not wrap to the next line when I reach the end of the line and it wraps onto the same line, overwriting my prompt and the rest of what I typed.
$... (5 Replies)
I have a script that checks if the script has been ran with sudo.
If the script is not ran as sudo, the current script is being executed with exec sudo bash.
You are asked for a password, you type in the password, success. Everything is perfect - the commands inside the script are ran as sudo.... (1 Reply)
Discussion started by: boqsc
1 Replies
LEARN ABOUT SUSE
bashbug
BASHBUG(1) General Commands Manual BASHBUG(1)NAME
bashbug - report a bug in bash
SYNOPSIS
bashbug [--version] [--help] [email-address]
DESCRIPTION
bashbug is a shell script to help the user compose and mail bug reports concerning bash in a standard format. bashbug invokes the editor
specified by the environment variable EDITOR on a temporary copy of the bug report format outline. The user must fill in the appropriate
fields and exit the editor. bashbug then mails the completed report to bug-bash@gnu.org, or email-address. If the report cannot be
mailed, it is saved in the file dead.bashbug in the invoking user's home directory.
The bug report format outline consists of several sections. The first section provides information about the machine, operating system,
the bash version, and the compilation environment. The second section should be filled in with a description of the bug. The third sec-
tion should be a description of how to reproduce the bug. The optional fourth section is for a proposed fix. Fixes are encouraged.
ENVIRONMENT
bashbug will utilize the following environment variables if they exist:
EDITOR Specifies the preferred editor. If EDITOR is not set, bashbug defaults to emacs.
HOME Directory in which the failed bug report is saved if the mail fails.
TMPDIR Directory in which to create temporary files and directories.
SEE ALSO bash(1)AUTHORS
Brian Fox, Free Software Foundation
bfox@gnu.org
Chet Ramey, Case Western Reserve University
chet@po.cwru.edu
GNU Bash-4.0 1998 July 30 BASHBUG(1)