Hi,
I try to write a C program which lists the output of a paticular command with all the available options (a to z) for the command in the directory of execution.
This program will generate the output if the option exists for the particular command else it will display some message saying "Option not available for the command".
ex: ls command has option "l" but not a option "y".
It will display the output on ls -l execution and error message on ls -y execution.
I am passing the "command to be executed(ex:ls,more)" in the command line argument. With in the program, I am using a while loop which initiated with 'a' and runs up to 'z'. I am using the "system()" unix call for the execution of command. please find my program below:
# include <stdio.h>
# include <stdlib.h>
# include <strings.h>
main(argc,argv)
int argc;
char *argv[];
{
int i;
printf("%d is arg\nc",argc);
printf("arg0 is %s\n",argv[0]);
printf("arg1 is %s\n",argv[1]);
if(argc!=2)
{
printf("usage Wrong");
exit(0);
}
printf("The command you ahve entered is %s\n",argv[1]);
printf("Let us display the various outputs for different options for the command");
i='a';
while(i>='a'&& i<='z')
{
printf("\n option is %d ASCII is %c",i,i);
system("cat > /tmp/cmd_opt.sh << EOF
echo cmd is $1
echo opt is $2
$1 -$2
EOF ");
system("sh /tmp/cmd_opt.sh argv[1] i");
i++;
}
}
on compiling the program with cc, its giving a warning message "multi-line string literals are deprecated"
But however on execution of the program, the output is as follows:
[ramki@lindesk3 ramki]$ cc options_wth_exe_cmds.c -o cmd_opt
options_wth_exe_cmds.c:28:16: warning: multi-line string literals are deprecated
[ramki@lindesk3 ramki]$ ./cmd_opt ls
2 is arg
carg0 is ./cmd_opt
arg1 is ls
The command you ahve entered is ls
Let us display the various outputs for different options for the command
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 97 ASCII is a
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 98 ASCII is b
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 99 ASCII is c
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 100 ASCII is d
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 101 ASCII is e
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 102 ASCII is f
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 103 ASCII is g
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 104 ASCII is h
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 105 ASCII is i
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 106 ASCII is j
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 107 ASCII is k
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 108 ASCII is l
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 109 ASCII is m
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 110 ASCII is n
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 111 ASCII is o
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 112 ASCII is p
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 113 ASCII is q
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 114 ASCII is r
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 115 ASCII is s
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 116 ASCII is t
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 117 ASCII is u
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 118 ASCII is v
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 119 ASCII is w
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 120 ASCII is x
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 121 ASCII is y
cmd is
opt is
/tmp/cmd_opt.sh: line 3: -: command not found
/tmp/cmd_opt.sh: line 4: EOF: command not found
option is 122 ASCII is z[ramki@lindesk3 ramki]$
Please help me in this..
Thanks,
Ramkrix.