Wrap __LINE__ in to single quotes - gnu

How can I wrap LINE and FILE macros in M4 to display '4' or "4". I want it to be ready for output to the console. I am using C++ for this.

Not 100% percent sure if this is what you're after but this makes __file__ to output filename in quotes (on my mac os):
define(`_m4__file__',defn(`__file__'))
pushdef(`__file__',`"_m4__file__"')
define(`_m4__line__',defn(`__line__'))
pushdef(`__line__',`"_m4__line__"')
define(`echo',`$*')
echo(__file__,__line__)
echo(__file__,__line__)
output:
"m4.t","8"
"m4.t","9"
for single quotes use a bit more escaping:
define(`_m4__file__',defn(`__file__'))
pushdef(`__file__',`''`_m4__file__'`'')
define(`_m4__line__',defn(`__line__'))
pushdef(`__line__',`''`_m4__line__'`'')
define(`echo',`$*')
echo(__file__,__line__)
echo(__file__,__line__)
outputs:
'm4.t','8'
'm4.t','9'

Related

How do I pass ">>" or "<<" to my script without the terminal trying to interpret it as me either appending to something or getting stdin?

My python script can take a series of bitwise operators as one of its arguments. They all work fine except for "=<<" which is roll left, and "=>>" which is roll right. I run my script like ./script.py -b +4,-4,=>>10,=<<1, where anything after -b can be any combination of similar operations. As soon as the terminal sees "<<" though, it just drops the cursor to a new line after the command and asks for more input instead of running the script. When it sees ">>", my script doesn't process the arguments correctly. I know it's because bash uses these characters for a specific purpose, but I'd like to get around it while still using "=>>" and "=<<" in my arguments for my script. Is there any way to do it without enclosing the argument in quotation marks?
Thank you for your help.
You should enclose the parameters that contain special symbols into single quotation marks (here, echo represents your script):
> echo '+4,-4,=>>10,=<<1'
+4,-4,=>>10,=<<1
Alternatively, save the parameters to a file (say, params.txt) and read them from the file onto the command line using the backticks:
> echo `cat params.txt`
+4,-4,=>>10,=<<1
Lastly, you can escape some offending symbols:
> echo +4,-4,=\>\>10,=\<\<1
+4,-4,=>>10,=<<1

converting a string passed from unix shell to a node program to a javascript-formatted one?

I'm using a bash script to send an argument to a node app like so:
testString="\nhello\nthere"
node ./myNodeScript.js $testString
The trouble comes when I use testString inside the node program after capturing it as process.argv[2] -- rather than expand the \n characters to newlines node prints them literally. I need a way to tell node to convert the argument to a javascript string, respecting the formatting characters. Is there a way to go about this?
Try to avoid confusing literal linefeeds and literal backslash followed by literal n.
If you want the string you pass to have linefeeds, you should ignore JavaScript string literal syntax and just pass the linefeeds as linefeeds:
$ cat myNodeScript.js
console.log("Node was passed this: " + process.argv[2])
$ cat myBashScript
testString='
hello
there'
printf 'Bash passes this: %s\n' "$testString"
node myNodeScript.js "$testString"
$ bash myBashScript
Bash passes this:
hello
there
Node was passed this:
hello
there
Arguments should contain data (linefeed) while script files should contain code (quoted linefeed or expanded \n as appropriate in the language). When you make sure not to confuse code and data, you can trivially handle both backslash-en and linefeeds in the same string with no surprises:
testString='
"\nhello\nthere" is JavaScript syntax for:
hello
there'
There are ways to express this on a single line in bash using \n for linefeeds and \\n for backslash-en, you just need to make sure that it remains as code, and doesn't accidentally make it into the variable as data.
Can you try this:
testString=$( printf "\nhello\nthere")
node ./myNodeScript.js "$testString"
And let me know if it works?

Backslashes in command-line argument under Cygwin

I am passing in the full path to a file as a commandline argument in perl.
For example
myscript.pl C:\Dir\myfile.txt
In myscript.pl, I have
my $full_path = shift;
print $full_path;
When I do this, my output is
C:Dirmyfile.txt
What I really want is C:\Dir\myfile.txt
But when I run my script as
myscript.pl 'C:\Dir\myfile.txt'
my output is C:/Dir/myfile.txt. Now it has forward slashes instead of backslashes. How do I get what I want? (The same text as what was passed in, file path with backslashes)
I need to run be able to run this script on Cygwin in a windows environment. Note that the script serves a larger purpose, but what I have posted is the part I am stuck with. The path is something I copy from somewhere else, so I really don't want to do the extra work of replacing backslash with forward slash or spaces.
use the File::Spec module. This simplifies passing parameters to your script, since you don't need to use slashes, and it also makes your application portable across operating systems.
use File::Spec;
my $full_path = File::Spec->catfile(#ARGV);
print $full_path, "\n";
Example:
perl myscript.pl C: Dir myfile.txt
C:\Dir\myfile.txt
Alternatively, if you need to use the full path string, then use the following line in place of the above:
my $full_path = File::Spec->canonpath($ARGV[0]);
Example 2:
perl myscript.pl C:\Dir\myfile.txt
--OR--
perl myscript.pl C:/Dir/myfile.txt
C:\Dir\myfile.txt
Example 3 (for Cygwin) - surround parameter with single quotes:
perl myscript.pl 'C:\Dir\myfile.txt'
C:\Dir\myfile.txt

How to call from within Python an application with double quotes around an argument using subprocess?

I'm trying to call certutil from inside python. However I need to use quotation marks and have been unable to do so. My code is as follows:
import subprocess
output= subprocess.Popen(("certutil.exe", "-view", '-restrict "NotAfter <= now+30:00, NotAfter >= now+00:00"' ), stdout=subprocess.PIPE).stdout
for line in output:
print(line)
output.close()
I thought the single quotes would allow me to use double quotes inside the string.
Also I have tried using double quotes with the escape character (\"), however I keep getting the same error:
Unknown arg: -restrict \\NotAfter\r\n'
For some reason it seems to be translating " into \\.
Can anyone give insight as to why and how to fix it?
I do not have Python in any version installed. But according to answer on How do I used 2 quotes in os.system? PYTHON and documentation of subprocess, subprocess handles requirement for double quotes on arguments with space automatically.
So you should need to use simply:
import subprocess
output= subprocess.Popen(("certutil.exe", "-view", "-restrict", "NotAfter <= now+30:00, NotAfter >= now+00:00" ), stdout=subprocess.PIPE).stdout
for line in output:
print(line)
output.close()
And the command line used on calling certutil is:
certutil.exe -view -restrict "NotAfter <= now+30:00, NotAfter >= now+00:00"
output=subprocess.Popen(("certutil.exe -view -restrict \"NotAfter<=now+30:00,NotAfter>=now+00:00\"" ),stdout=subprocess.PIPE).stdout
This is what was needed. I was passing the command as a command with 2 args. What I should have been doing was passing it as one big command with 2 parameters.

Semicolon on command line in linux

i am running my application in linux by providing inputs as command line. My input field contain an argument which contains ";"(semicolon) internally.(For example:123;434;5464).
This will be parsed using UTF8String encode and send.
But when i am using like this, in initial itself i am getting,
bash: 434: command not found
bash: 5464: command not found
And when i capture traffic the output contains only 123 instead 123;434;5464
But if i give without semicolon (Ex:123:434:5464),not getting any problem output coming properly as 123:434:5464
Point me how to give command line input by using semicolon as to come output. Is there any particular syntax to use while doing with semicolon.
I am running like below
./runASR.sh -ip 10.78.242.4 -port 3868 -sce 10.78.241.206 -id 85;167838865;1385433280
where -id field contain that value with issue.
; is treated an end of command character. So 123;456;5464 to bash is in fact 3 commands. To pass such meta-characters escape it with escape character \.
./command 123\;456\;5464
Or Just quote it with single quote (double quote evaluates the inner string) (Thanks Triplee, I forgot to mention this)
./command '123;456;5464'

Resources