LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash: question about command line parsing (https://www.linuxquestions.org/questions/programming-9/bash-question-about-command-line-parsing-4175664108/)

igadoter 11-12-2019 09:40 AM

Bash: question about command line parsing
 
Hi, say last command was
Quote:

$ ls -l
is there a way to find what was last command and its parameters and arguments? In shell script this is accessible by positional parameters $0 etc - is there something for command line?

ehartman 11-12-2019 09:46 AM

Quote:

Originally Posted by igadoter (Post 6056796)
is there something for command line?

Try 'history'
Read the bash man page or a manual to find out which settings influence the retainment of history (hint: they start with HIST, in capitals).

rtmistler 11-12-2019 09:49 AM

It depends what you want to do. You can seemingly, and easily "run" the last command using things like !! or !-1. If you want to contents of the last command, there's a trap capability in bash which you can use. Seems a lot of work, but I've seen it done.

https://www.ostechnix.com/5-ways-rep...command-linux/
https://stackoverflow.com/questions/...nd-run-in-bash

MadeInGermany 11-12-2019 10:45 AM

Besides the history command there are some shortcuts, for example !! and !*
  • Echo the last command (with arguments):
  • Code:

    echo !!
  • Echo the last command's arguments:
  • Code:

    echo !*

rknichols 11-12-2019 11:10 AM

You can also use the up-arrow key to scroll back through the command history.

igadoter 11-12-2019 11:41 AM

Quote:

Originally Posted by MadeInGermany (Post 6056822)
Besides the history command there are some shortcuts, for example !! and !*
  • Echo the last command (with arguments):
  • Code:

    echo !!
  • Echo the last command's arguments:
  • Code:

    echo !*

These are good tricks. This is what I needed
Code:

$ ls -l
....
$ FOO=($( echo !!))
$ echo ${FOO[0]}
ls
$ echo ${FOO[1]}
-l

great thanks!

rtmistler 11-12-2019 11:42 AM

The problem with each of !! and history, you get a list. Well, not a problem if you process it within your script.

Just noting that I think that igadoter wishes to do this operation within a script. And also that they haven't cited how they intend to use it exactly. It may be fine to use !! to re-run the prior command. Or if they wish to know what that former command was and store, modify, process it somehow, then I believe echo'ing !! will give you both the echo !! as well as the prior command.

igadoter 11-13-2019 09:25 AM

Quote:

Originally Posted by rtmistler (Post 6056840)
Just noting that I think that igadoter wishes to do this operation within a script.

Nope. Take a script and do this
Code:

$ while read LINE ; do echo $LINE ; done < my_bash_script
you will see that eg. line splitting \ at end of line are removed. Dropping all comments one sees what bash sees. But say I need to go further and parse each $LINE for command and options, arguments - best solution is employ bash to do the job instead trying to write awk script or something like that to look for patterns and accordingly split line. Essentially I need to extract something from a script.

MadeInGermany 11-13-2019 01:13 PM

Quote:

Originally Posted by igadoter (Post 6056839)
These are good tricks. This is what I needed
Code:

$ ls -l
....
$ FOO=($( echo !!))
$ echo ${FOO[0]}
ls
$ echo ${FOO[1]}
-l

great thanks!

A bit simpler is
Code:

FOO=(!!)

igadoter 11-13-2019 03:06 PM

Quote:

Originally Posted by MadeInGermany (Post 6057317)
A bit simpler is
Code:

FOO=(!!)

That rocks. Thanks for all of you. Thread give me idea to use this
Code:

$ while read -a LINE ; do echo ${#LINE[@]}: ${LINE[@]} ; done < my_bash_script
look at this
Code:

55: find -L . ( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 -o -perm 511 ) -exec chmod 755 {} ; -o ( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 ) -exec chmod 644 {} ;
in script is split over several lines - which does not makes script easier to understand. Now just match every member of LINE array.

Firerat 11-13-2019 07:20 PM

hmm

it looks like you actually want something like this

Code:

bash -x ./my_bash_script 2>debug.log
each command and expanded arguments will be printed to stderr ( which I'm redirecting to a file with 2> )

you can also use within script
Code:

set -x # turn it on
set +x # turn if off

you could make those conditional with an arg. passed to script
I mostly just "go the whole hog" with bash -x
but using set within the script to reduce the noise


but I may be a little confused about what you are actually trying to achieve.

igadoter 11-14-2019 04:24 AM

Quote:

Originally Posted by Firerat (Post 6057426)
hmm

it looks like you actually want something like this

Code:

bash -x ./my_bash_script 2>debug.log
each command and expanded arguments will be printed to stderr ( which I'm redirecting to a file with 2> )

I don't want script to be executed. This site www.slackbuilds.org provides number of scripts to easily build application from source. Inside each script there are set compiler flags and build flags. I just want to extract these data from script. My first thought was to look for regexp, cat, grep combination or awk. The first problem was line which with line continuation sign \ - long lines are being split inside scripts - but this makes more difficult to parse lines. Then I found simple solution to reread script with while read ; do done < script. Better however is to use while read -a ; do done < script. Say --sysdir flag is now array element. So flags, parameters are elements which should match pattern ^-

Firerat 11-14-2019 05:29 AM

yeah, thought I was miss understanding something

Code:

sed -e :a -e '/\\$/N; s/\\\n//; ta' inputfile
from http://sed.sourceforge.net/sed1line.txt


All times are GMT -5. The time now is 09:49 PM.