Need some help with this...


 
Thread Tools Search this Thread
Top Forums Programming Need some help with this...
# 15  
Old 09-30-2007
Basically you would be converting the following into C.

Code:
#!/bin/sh

first()
{
        echo $1
}

for n in $@
do
        /usr/bin/alias | while read STR
        do
                NAME=`echo $STR | sed y/=/\ /`
                FIRST=`first $NAME`
                if test "$FIRST" = "$n"
                then
                        echo $STR | sed s/$n=//
                fi
        done

        if test -f $n
        then
                echo $n
        fi

        for d in `echo $PATH | sed y/:/\ /`
        do
                if test -f $d/$n
                then
                        echo $d/$n
                fi
        done
done

# 16  
Old 09-30-2007
I'm not sure if I still know what I need to do.. I'll just go through that code for now...
# 17  
Old 09-30-2007
which is supposed to tell you what actual command will be executed if you type it in.

So if the name is listed by alias then there is an alias mapping which is run instead of looking for the command in the $PATH. Presumeably you are supposed to print out that mapping?
# 18  
Old 09-30-2007
Smilie I think I got you now... I'll try writing for that... Thank You... Will get back when I'm done with it...
# 19  
Old 09-30-2007
I tried out a short example:

Code:
 fp = popen("alias","r");
            if(fp == NULL)
            {
                  printf("Handle Error");
            }
            printf("You asked for an alias");
            while(fgets(alia,20,fp) != NULL)
            printf("%s",alia);
            pclose(fp);

It goes upto "You asked for an alias" and quits... alia is a "char alia[1024];" definition and fp is a file pointer... Am I doing something wrong?

Edit: It outputs the data if I use something else other than alias. For example, something like netstat. I'm getting some error when I try to directly put something like alias ls. It says:

sh: line 1: alias: ls: not found

Last edited by Legend986; 09-30-2007 at 08:33 PM..
# 20  
Old 09-30-2007
Quote:
Originally Posted by Legend986
sh: line 1: alias: ls: not found
Chicken and egg.

Perhaps your system does not support alias...?

try

Code:
which alias

similarly

Code:
bash -c alias
ksh -c alias
zsh -c alias
sh -c alias
csh -c alias

# 21  
Old 09-30-2007
sh and bash do not produce any outputs whereas the others produce. The alias command works if I type it outside the program but "which alias" says it cannot find the command.
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question