LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-12-2019, 09:40 AM   #1
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,717
Blog Entries: 1

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
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?
 
Old 11-12-2019, 09:46 AM   #2
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
Quote:
Originally Posted by igadoter View Post
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).
 
Old 11-12-2019, 09:49 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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
 
Old 11-12-2019, 10:45 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,791

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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 !*
 
Old 11-12-2019, 11:10 AM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
You can also use the up-arrow key to scroll back through the command history.
 
Old 11-12-2019, 11:41 AM   #6
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,717

Original Poster
Blog Entries: 1

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
Quote:
Originally Posted by MadeInGermany View Post
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!
 
Old 11-12-2019, 11:42 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
Old 11-13-2019, 09:25 AM   #8
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,717

Original Poster
Blog Entries: 1

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
Quote:
Originally Posted by rtmistler View Post
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.
 
Old 11-13-2019, 01:13 PM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,791

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Quote:
Originally Posted by igadoter View Post
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=(!!)
 
1 members found this post helpful.
Old 11-13-2019, 03:06 PM   #10
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,717

Original Poster
Blog Entries: 1

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
Quote:
Originally Posted by MadeInGermany View Post
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.
 
Old 11-13-2019, 07:20 PM   #11
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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.
 
Old 11-14-2019, 04:24 AM   #12
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,717

Original Poster
Blog Entries: 1

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
Quote:
Originally Posted by Firerat View Post
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 ^-
 
Old 11-14-2019, 05:29 AM   #13
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
yeah, thought I was miss understanding something

Code:
sed -e :a -e '/\\$/N; s/\\\n//; ta' inputfile
from http://sed.sourceforge.net/sed1line.txt
 
1 members found this post helpful.
  


Reply

Tags
bash



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Parsing mountpoint from /etc/mtab (parsing "string") in C deadeyes Programming 3 09-06-2011 05:33 PM
grab the line below a blank line and the line above the next blank line awk or perl? Pantomime Linux - General 7 06-26-2008 08:13 AM
Parsing text file line by line armandino101 Linux - Newbie 3 12-14-2006 02:43 PM
Parsing a string line-by-line in PHP enigma_0Z Programming 3 04-21-2006 08:07 AM
Mozilla Linux Command Line URL Parsing Security Flaw Reported win32sux Linux - Security 9 10-06-2005 06:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration