LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 10-21-2017, 12:39 PM   #1
keithpeter
Member
 
Registered: Nov 2015
Location: 52:30N 1:55W
Distribution: Slackware 15.0, OpenBSD 7.4
Posts: 310

Rep: Reputation: Disabled
Bash brainteaser: get raw string from command line [no parameter expansion]


Hello All

This is not strictly a Slackware issue and mods might want to move this post.

Is it possible to access the raw string that a user typed into the command line after the program/script name without any form of globbing or parameter expansion so that the 'special characters' are not special until <newline>?

I'm trying to write a script/program called notes that will be invoked as below in a terminal

Code:
notes "That's impossible!" he said, as well as invoking the $PATH variable and 'hinting' avidly
and append this one line entry in a file (.notes say)

Code:
20171021183845 "That's impossible!" he said, as well as invoking the $PATH variable and 'hinting' avidly
The nearest I can get to on the Interwebs is this page

https://qntm.org/bash

The closest I have got is a heredoc but that has double-quote issues and requires a three line session.

(I would want to prepend each line in .notes with a time stamp)

Background...

https://www.linuxquestions.org/quest...ml#post5772301

Last edited by keithpeter; 10-21-2017 at 01:17 PM. Reason: Removed a line that could be found potentially patronising
 
Old 10-21-2017, 01:20 PM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,848

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
I do not really understand your problem:
Code:
user@host:/$ read a
asdfasdf@':dsfgadsf""/asdrfqw4rf   \fr \\er
user@host:/$ echo "$a"
asdfasdf@':dsfgadsf""/asdrfqw4rf   fr \er
user@host:/$
user@host:/$ read a
ldafshblaerfj\
> adfv vawsre vav\adrvqw 34iu {asdgf wrg} dg ((ar gerg @#$!#$^ asdfvg } \\ '''
user@host:/$ echo $a
ldafshblaerfjadfv vawsre vavadrvqw 34iu {asdgf wrg} dg ((ar gerg @#$!#$^ asdfvg } \ '''
user@host:/$
This way only \ (and newline) cannot be catched (all the other characters worked for me) but I think they are processed by the shell before anything else, so you cannot avoid that.
You need to write c code to catch every keypress.

Last edited by pan64; 10-21-2017 at 01:22 PM.
 
Old 10-21-2017, 01:29 PM   #3
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 490
Blog Entries: 4

Rep: Reputation: 238Reputation: 238Reputation: 238
This is as close as i can get. Wrote a simple c program ("printarg.c) to echo the command line argument, and had to follow the exclamation point with a blank space.
Code:
printarg "\"That's impossible\! \", he said, as well as invoking the \$PATH variable and 'hinting' avidly"
 
Old 10-21-2017, 01:35 PM   #4
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
Whatever you type on that command line is first going to be processed by the shell, which will do its own handling of quoting, variable expansion, and other special characters. There is nothing you can do in the script to reverse that and get back exactly what was typed. What you can do is invoke the script with no argument, and then within the script use the read command to read the line. Appending that line literally to the file is then not a problem.
Code:
#!/bin/sh
read -r -p 'Note: ' Line
echo $(date +%Y%m%d%H%M%S) "$Line" >>.notes
Actually, that still trims off any leading or trailing white space on the line. To avoid that, use a null IFS:
Code:
#!/bin/sh
IFS= read -r -p 'Note: ' Line
echo $(date +%Y%m%d%H%M%S) "$Line" >>.notes
 
Old 10-21-2017, 01:50 PM   #5
Diantre
Member
 
Registered: Jun 2011
Distribution: Slackware
Posts: 515

Rep: Reputation: 234Reputation: 234Reputation: 234
Code:
$ echo '"That'"'"'s not impossible!" he said.'
From this page.

But I'd rather use the "read" builtin instead as mentioned by others here, if your intention is to write a note. Less headaches not having to think how to quote something.
 
Old 10-21-2017, 02:22 PM   #6
keithpeter
Member
 
Registered: Nov 2015
Location: 52:30N 1:55W
Distribution: Slackware 15.0, OpenBSD 7.4
Posts: 310

Original Poster
Rep: Reputation: Disabled
Hello all

Yes, you are (all) correct. Can't get the raw keypresses using native bash commands on the command line.

I'm going to have to accept either typing escape codes in the notes or going for multi-line input of some kind. Looks like I'm going down rknichols' line.

Thanks. I'll leave the brain teaser up for a bit to see if anyone comes up with something amazing.

Diantre has hit the nail on the head: I was trying to 'suspend' any form of parameter expansion/globbing so as to keep typing easier.

Last edited by keithpeter; 10-21-2017 at 02:24 PM.
 
Old 10-21-2017, 09:41 PM   #7
Paulo2
Member
 
Registered: Aug 2012
Distribution: Slackware64 15.0 (started with 13.37). Testing -current in a spare partition.
Posts: 928

Rep: Reputation: 515Reputation: 515Reputation: 515Reputation: 515Reputation: 515Reputation: 515
The command line goes to bash history as it is typed. Does this help?
Code:
$ echo "That's impossible!" he said, as well as invoking the $PATH variable and 'hinting' avidly
That's impossible! he said, as well as invoking the /usr/local/bin:/usr/bin:/bin:/usr/games:/usr/lib64/kde4/libexec:/usr/lib64/java/bin:/usr/lib64/java/jre/bin:/scripts:/usr/lib64/qt/bin variable and hinting avidly
$ history 2
  510  echo "That's impossible!" he said, as well as invoking the $PATH variable and 'hinting' avidly
  511  history 2
 
2 members found this post helpful.
Old 10-22-2017, 05:06 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,848

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
I see here a very important issue: echo will not give you the content of the variable, it will be immediately evaluated (before printing).
For example echo $VAR will print the value of VAR, echo * will print the files in the current directory. If you want to print the content of the variable, use ":
echo "$VAR" will probably do what you want.
From the other hand you want to assign a value. If you want input use read as I posted before. If you want to assign, use ' again:
Code:
a='"That'"'"'s impossible!" he said, as well as invoking the $PATH variable and '"'"'hinting'"'"' avidly'
only the original ' should be tricked by: ' " ' " ' .
 
Old 10-22-2017, 06:02 AM   #9
kjhambrick
Senior Member
 
Registered: Jul 2005
Location: Round Rock, TX
Distribution: Slackware64 15.0 + Multilib
Posts: 2,159

Rep: Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512
keithpeter --

EDIT: Thanks to Paulo2 for the ~/.bash_history trick ! I'll try to remember that one !!

You didn't mention whether your notes app needed to be scriptable or not ...

I don't know offhand how to write a scriptable script that can 'reach upstream' and turn off bash input processing for it's own commandline ...

However, if your notes app can be an interactive app then this might work with a little work.

Save the following as say, notes.sh ...

Code:
#!/bin/sh

NoteFile="/tmp/notes.txt"   # edit me ...

echo -e "\ntype your notes ; then press [Ctrl] D to exit\n" >&2 

cat | \
gawk '
BEGIN {
 
   Time = strftime( "%Y%m%d%H%M%S" ) " " 
}
# main 
{ 
   print Time $0 

}' >> $NoteFile
Usage:
Code:
notes.sh                    # press Enter
type your notes ...         # when done, press {Ctrl] D ( aka eof )
What did we get ?

Code:
$ cat /tmp/notes.txt

20171022054632 "That's impossible!" he said, as well as invoking the $PATH variable and 'hinting' avidly
20171022054654 "That's impossible!" he said, as well as invoking the $PATH variable and 'hinting' avidly
You can use your mouse to cut-n-paste text into the `cat` input stream ( as I did ).

Sorry, best I can do

-- kjh

Last edited by kjhambrick; 10-22-2017 at 06:12 AM. Reason: add p,s, about Paulo2's ~/.bash_history trick
 
Old 10-22-2017, 06:06 AM   #10
keithpeter
Member
 
Registered: Nov 2015
Location: 52:30N 1:55W
Distribution: Slackware 15.0, OpenBSD 7.4
Posts: 310

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Paulo2 View Post
The command line goes to bash history as it is typed. Does this help?
It certainly does! And that solves the reading notes back problem as well, I can just use Ctrl-R search. Thanks very much.
 
Old 10-22-2017, 06:09 AM   #11
keithpeter
Member
 
Registered: Nov 2015
Location: 52:30N 1:55W
Distribution: Slackware 15.0, OpenBSD 7.4
Posts: 310

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by kjhambrick View Post
keithpeter --
...

However, if your notes app can be an interactive app then this might work with a little work.

...

-- kjh
Thanks very much, I shall have a play. The original concept was to type the command and then the note on one command line using the line editing commands that bash provides. The nearest thing looks like the read command used interactively as you say. Your conditioning of the text input is educational and I'll dig into awk/sed a bit more
 
Old 10-22-2017, 06:13 AM   #12
keithpeter
Member
 
Registered: Nov 2015
Location: 52:30N 1:55W
Distribution: Slackware 15.0, OpenBSD 7.4
Posts: 310

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
I see here a very important issue: echo will not give you the content of the variable, it will be immediately evaluated (before printing).
For example echo $VAR will print the value of VAR, echo * will print the files in the current directory. If you want to print the content of the variable, use ":
echo "$VAR" will probably do what you want.
From the other hand you want to assign a value. If you want input use read as I posted before. If you want to assign, use ' again:
Code:
a='"That'"'"'s impossible!" he said, as well as invoking the $PATH variable and '"'"'hinting'"'"' avidly'
only the original ' should be tricked by: ' " ' " ' .
Thanks pan64. Your read based solution is the one currently in use.

The idea was to avoid possible random behaviour by accidently typing commands in the text of a note! Remembering the escape sequences correctly could be tricky when in a hurry.

I have three solutions to try now so marking this as solved. Thanks all, very educational.
 
Old 10-22-2017, 06:27 AM   #13
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
Relying on bash history is ingenious but likely to be fragile. I'd stick with reading from stdin if I were you.

Quote:
This is the Unix philosophy: Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface.
Words to live by.
 
1 members found this post helpful.
Old 10-22-2017, 07:02 AM   #14
kjhambrick
Senior Member
 
Registered: Jul 2005
Location: Round Rock, TX
Distribution: Slackware64 15.0 + Multilib
Posts: 2,159

Rep: Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512Reputation: 1512
YW, keithpeter

Thank you for the brainteaser !

The following sequence works but only if you either source notes.sh ( or type: . notes.sh )

Paulo2's hint about the history command does do the deed ( ! thanks again Paulo2 ! )

Here is a session ( I did it twice to see it work )

Code:
$  echo "That's impossible!" he said, as well as invoking the $PATH variable and 'hinting' avidly

That's impossible! he said, as well as invoking the /home/local/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/games:/root/bin:/usr/lib64/java/bin:/usr/lib64/kde4/libexec:/usr/lib64/qt/bin:/usr/lib64/qt5/bin:/usr/share/texmf/bin variable and hinting avidly

$ . notes.sh   # note the dot-space invocation ( i.e. be sure to source /tmp/motes.sh into bash )

$ cat /tmp/notes.sh

20171022064629 echo "That's impossible!" he said, as well as invoking the $PATH variable and 'hinting' avidly
20171022064654 echo "That's impossible!" he said, as well as invoking the $PATH variable and 'hinting' avidly
-- kjh

this is /tnp/notes.sh
Code:
#!/bin/sh
#
# notes.sh - appends the preceeding commandline into $NoteFile
#
# source the script ( . notes.sh -or- source notes.sh )

NoteFile="/tmp/notes.txt"   # edit me ...

history 2 |
gawk '
BEGIN {
 
   Time = strftime( "%Y%m%d%H%M%S" ) " " 
}
# main 
{ 
   Cmd = $0

   gsub( /^ *[0-9]* */, "", Cmd )

   print Time Cmd
 
   exit( 0 )       # print the first line only ( skip the history cmd at eof )

}' >> $NoteFile

return 0 2>/dev/null || exit 0
 
Old 10-22-2017, 07:51 AM   #15
bassmadrigal
LQ Guru
 
Registered: Nov 2003
Location: West Jordan, UT, USA
Distribution: Slackware
Posts: 8,792

Rep: Reputation: 6656Reputation: 6656Reputation: 6656Reputation: 6656Reputation: 6656Reputation: 6656Reputation: 6656Reputation: 6656Reputation: 6656Reputation: 6656Reputation: 6656
Quote:
Originally Posted by pan64 View Post
I see here a very important issue: echo will not give you the content of the variable, it will be immediately evaluated (before printing).
For example echo $VAR will print the value of VAR, echo * will print the files in the current directory. If you want to print the content of the variable, use ":
echo "$VAR" will probably do what you want.
If you don't want variable expansion with echo use single quotes instead of double quotes (although, any quotes will prevent * from being expanded).

Code:
jbhansen@febtober:~$ echo $USER
jbhansen
jbhansen@febtober:~$ echo "$USER"
jbhansen
jbhansen@febtober:~$ echo '$USER'
$USER
jbhansen@febtober:~$ echo *
: Desktop Documents Downloads Music Pictures Public SBo-slackbuilds Templates Videos VirtualBox VMs backup boot edid.bin filebot games gparted-live-0.27.0-1-amd64.iso htpc-prep install iso2usb.sh keymap kodi_crashlog-20170423_205246.log latest-firefox.sh liveslak mirror_test.sh mozilla-firefox nohup.out program-downloads robots.txt sbopkg-0.38.0-noarch-1_wsr.tgz scripts slackbuilds slackrepo-hintfiles-14.2 tmp videos
jbhansen@febtober:~$ echo "*"
*
jbhansen@febtober:~$ echo '*'
*
 
2 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] Getting a certain Parameter from the command-line of a foreign process in bash... nachaz Linux - Newbie 4 01-02-2012 03:52 PM
Does bash shell parameter expansion run the risk of overflowing command line buffer? david1941 Linux - Software 1 04-28-2009 10:13 PM
LXer: Bash Parameter Expansion LXer Syndicated Linux News 0 10-02-2008 03:40 PM
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 04:54 AM
bash shell command line expansion hansi umayangan Linux - General 2 03-13-2005 11:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 04:55 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