Random unix question from test

Associate
Joined
13 Jan 2007
Posts
2,424
Location
Belfast,Northern Ireland
Had a recent test at work and of course missed the pass mark by 1 question, Im good like that. Anyway when I think back there's only one question I felt pretty unsure of:

What does the command !?abc?* do?
1. Nothing, this is invalid syntax
2. Appends * to the most recent command containing the symbols !?
3. Appends * to the most recent command containing the string abc
4. Searches for all files with abc in their heading

There is of course only one correct answer. I went for number 1, anyone able to shed light if I was correct or not?
 
Incorrect i'm afraid, 3 is the correct answer (i think)

The command searches the event log (bash history) for a command containing the substring "abc", I thought the * would be part of a regex rather than being concatenated, guess not :p.
 
I'm not so sure ... without the * it behaves as you say but adding the * seems to break it ....

Code:
[~] # cat index_default.html
(snipped)
[~] # sh --version
GNU bash, version 3.2.0(4)-release (i686-pc-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
[~] # !?ver?*
--version
-sh: --version: command not found
[~] # !?cat?*
index_default.html
-sh: index_default.html: command not found
[~] # ls /root
index_default.html
[~] # !?ls?*
/root
-sh: /root: is a directory
[~] # !?ls?
ls /root
index_default.html
So it would appear that, at least with the shell here (*), it is trying to execute the parameter given to the command matching the search (a "df" followed by the command with df in it returns a blank line). This is unlikely to work giving answer 1?

(*) it's a shell on a NAS so ymmv with a full linux OS.
 
I've just checked the bash manual for this, So * is a 'word designator' defined as follows:

Word designators are used to select desired words from the event. A ‘:’ separates the event specification from the word designator. It may be omitted if the word designator begins with a ‘^’, ‘$’, ‘*’, ‘-’, or ‘%’. Words are numbered from the beginning of the line, with the first word being denoted by 0 (zero). Words are inserted into the current line separated by single spaces.

*
All of the words, except the 0th. This is a synonym for ‘1-$’. It is not an error to use ‘*’ if there is just one word in the event; the empty string is returned in that case.

So, the * is used to retrive only the args of the matched command as you said. I guess you could say !?abc?* on it's own is invalid. But this works:

Code:
[jack@tmain pyusb]$ ls -la
total 24
drwxr-xr-x  6 jack users 4096 Aug 12 16:08 .
drwx------ 53 jack users 4096 Sep 12 19:41 ..
drwxr-xr-x  4 jack users 4096 Aug 12 16:08 branches
drwxr-xr-x  6 jack users 4096 Aug 12 16:08 .svn

[jack@tmain pyusb]$ ls !?ls?*
ls -la
total 24
drwxr-xr-x  6 jack users 4096 Aug 12 16:08 .
drwx------ 53 jack users 4096 Sep 12 19:41 ..
drwxr-xr-x  4 jack users 4096 Aug 12 16:08 branches
drwxr-xr-x  6 jack users 4096 Aug 12 16:08 .svn


Tricky one! It isn't invalid syntax, but it needs a prefix command, unless of cause the arg string is an executable in your path in which case it would work.
e.g
Code:
[jack@tmain pyusb]$ touch ls
[jack@tmain pyusb]$ ls ls
ls
[jack@tmain pyusb]$ !?ls?*
ls
branches  ls  tags  trunk
 
Last edited:
Back
Top Bottom