LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell scripting/ the fc -l command ! (https://www.linuxquestions.org/questions/linux-newbie-8/shell-scripting-the-fc-l-command-610809/)

mayaabboud 01-03-2008 07:36 AM

shell scripting/ the fc -l command !
 
i wrote the following program:

# !/bin/bash
#profile program
function histoire {
HISTSIZE=20
echo “HISTSIZE est égale à” $HISTSIZE
echo “le numéro de la commande est” $fc $-l
}
histoire
exit 0

can someone tell me, why am i not getting the same result, as in when i directly write the command fc -l ? and whats hBl ?

knoppix@knoppix:/ramdisk/home/knoppix/tmp$ chmod +x .profile
+ chmod +x .profile
knoppix@knoppix:/ramdisk/home/knoppix/tmp$ ./.profile
+ ./.profile
HISTSIZE est égale à 20
le numéro de la commande est hBl

acid_kewpie 01-03-2008 07:44 AM

where did you read that "$fc $-l" would do what you want? that means nothing.... try "$((fc -l))" instead.

mayaabboud 01-03-2008 07:49 AM

well after trying $(fc -l )
the only thing that wouldnt give me errors was $fc$-l !!

i tried $((fc -l)), but that gives me 0, i want the same list that i get when i simply type fc -l on the xterm !

how can i do that? any idea ?

acid_kewpie 01-03-2008 07:55 AM

yeah it does doesn't it? retard alert.... i knew it didn't look right... sorry, it does basic expression evalution...

# echo $(( 10 + 10 ))

20

you do need $(fc -l) or `fc -l` to actually execute fc as a command. i don't think that that's really useful to you though as it'll print it all on one line... i'd just put the fc command on a new line by itself.



doh! :)

mayaabboud 01-03-2008 08:01 AM

neither 'fc -l' nor $(fc -l) worked !
i already tried those two,
maybe i should add some argument or sthg? i cant get the answer that i want,

$(fc -l) gives me hBl as an answer !!
and 'fc -l' gives me fc -l as an answer !!

mayaabboud 01-03-2008 08:03 AM

actually $fc $-l gives me hBl as an answer
$(fc -l) gives me nth as an answer !!

acid_kewpie 01-03-2008 08:18 AM

no it's `fc -l` not 'fc -l' but as above i don't think you should use that mechanism at all, just run it normally outside of echo.

mayaabboud 01-03-2008 08:38 AM

what do you mean by outside of echo ?

i have this program:i changed the fc -l command :

# !/bin/bash
#profile program
function histoire {
HISTSIZE=20
echo “HISTSIZE est égale à” $HISTSIZE
echo “le numéro de la commande est” `fc -l`
}
histoire
exit 0

BUT I GET NOTHG AS AN ANSWER !

acid_kewpie 01-03-2008 08:42 AM

# !/bin/bash
#profile program
function histoire {
HISTSIZE=20
echo “HISTSIZE est égale à” $HISTSIZE
echo “le numéro de la commande est”
fc -l
}
histoire
exit 0

mayaabboud 01-03-2008 08:53 AM

this also gives me "nthg" as an answer !

acid_kewpie 01-03-2008 08:54 AM

do you atually mean "nothing"...? or literally a text string as you wrote?? if fc -l doesn't give you an output, then you have no recorded history to output...

mayaabboud 01-03-2008 09:08 AM

yes i actually mean nthg, just blank space ,
i have a recorded history to output, and i know that because when i type fc -l in my xterm then i get a long list !!

osor 01-03-2008 06:38 PM

Quote:

Originally Posted by mayaabboud (Post 3009666)
can someone tell me, why am i not getting the same result, as in when i directly write the command fc -l ? and whats hBl ?

Both of these questions have simple explanations. First, let’s see what fc does (taken from here):
Quote:

Originally Posted by The Open Group Base Specifications Issue 6
The fc utility shall list, or shall edit and re-execute, commands previously entered to an interactive sh.

As you can see, fc only works (or should only work) on an interactive shell (i.e., one in which you can interactively type commands).

For example, consider this simple program (which I will save in a text file named program):
Code:

#!/bin/sh

fc -l

Now, watch what happens when I try to execute it as a script:
Code:

$ chmod +x program
$ ./program
$

And watch what happens if I source it into my interactive shell:
Code:

$ source program
1        cd /tmp
2        cat > program
3        chmod +x program
4        ./program
$

As for “hBl”, this is the result expanding “$-l” in a script. You see, it shall be parsed as “${-}l” where the internal variable $- has the flags currently set (this is a ksh-ism borrowed by bash). In this case, the flags set are for brace expansion and command hashing.


All times are GMT -5. The time now is 11:47 PM.