[SOLVED] Bash: question about command line parsing
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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).
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.
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.
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.
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.
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 ^-
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.