How to use base64 encoded image as an argument? - shell

I am trying to use the base64 encoding of an image as a flag when I run my program. Im getting back: Argument list too long
I am on a Ubuntu 16.04 Docker image on a mac.
$ ./myProgram -input "/9j/4AAQSkZJRgABA [...]"

Instead of accepting it as an argument, I would suggest you read it from standard in instead.
$ base64 someImage.jpg | ./myProgram
If this program is a shell script, you can save standard in into a variable with something like this:
#!/bin/sh
MY_BASE64_IMAGE_INPUT=$(cat -)
# do something with that info
echo $MY_BASE64_IMAGE_INPUT

There is a length limit of a single command. This is your program... It should be easier in every way to send the name of the file as the argument to your program which would then read the desired information from the file. You could cat the contents of the file into a variable (if you really want it as a variable). It may be more conventional if you passed the contents of the file to your program via the standard input stream.

Related

How to pipe output of a command that expects file argument

I know you can pipe output of a one command to another - for example
ls -la | less
to see output of ls -la inside of less instead of terminal stdio.
But if you use a command with a parameter that saves the output to a file
command --save-to-file file.txt
Then how to pipe that to another command?
this way will not work:
command --save-to-file | less
because command will complain that you used --save-to-file without any argument (filename)
If I remember well there was something like a buffer or a temp file in ram you could put instead of file.txt so you could do something like:
command --save-to-file ram-buffer.txt && cat ram-buffer.txt
Without even creating a file on disk, it that right?
Why I need that?
Some of commands have only a basic output to the stdio and more useful type of output cannot be printed by them but only saved to file. The thing is I am not interested in saving the more useful type of output to any file at all but just to print it in terminal or pipe to chain of another commands that do the filtering etc and then eventually print the processed output.
I would not like to be responsible to crating a tmp file then delete it etc. Perfectly I would like to just use a kind of magic file (or redirection) in place of file.txt that I could pipe to another command.
It is important to me to not write any content of the output to a disk if this is possible. Just print it in terminal or pipe to other command(s).
At this moment I'm trying to capture output of PHPUnit
phpunit --log-junit log.xml
which is not a shell command but a PHP script that uses:
#!/usr/bin/env php
But I remember I used to have an example with linux command that I wanted get the output but the form of it was only available with a parameter --save-to-file outputfile.txt
Perhaps because piping/redirecting an output designed to be saved to a file is not binary safe and therefore such output can be corrupted when piped/redirected - can it be?
Some programs have special handling for -. For example, you can tell tar to write to stdout so it can be used in a pipeline. This would create a tarball locally and untar it remotely without the tarball ever being written to disk:
tar -cf - *.txt | ssh user#host tar -C /dir/ -xf -
You can use /dev/stdout with nearly all programs, as long as they don't need a seekable file.
command --save-to-file /dev/stdout
As #Benjamin W. pointed out in the comments, you can save it to /dev/stdout, which is the output and then pipe the output to whatever you want(e.g. less)
command --save-to-file /dev/stdout | less
Take care because there may be additional output to stdout. In this case you could throw that away, save it to stderr and redirect it to stdout:
command --save-to-file /dev/stderr >/dev/null 2>/dev/stdout | less
If both, stderr and stdout are used you may be able to write your own driver for this of manipulate /proc/pid/mem or something like this.

Piping input from a file to a command in windows cmd

My understanding is that the redirection operator, <, should allow me to take text from a file and give it as input to another file as if I had written out the contents of that file. Here is what I am trying to do:
python code.py < input.txt
I expect this to act as though I had typed the contents of input.txt after python code.py, but instead it acts as if I passed no input.
If I use cat, I get the contents of the file:
> cat input.txt
['2015-1-1','2015-5-1','2015-9-1','2015-10-1','2015-12-1','2016-1-1','2016-2-1','2016-4-1','2016-5-1'] [65,50,30,45,55,39,45,30,20]
And if I just copy and paste the contents of the file, I get the correct behavior.
I know this must be a really simple misunderstanding on my part, but I can't figure it out.
It's called Redirection, not piping, but you are correct that the < operator will push the file to the command. You can see this in action by using Sort instead of echo.
sort < input.txt
This will display the text file as a list, sorted alphabetically. Echo does not work with text files, so sending a text file to Echo simply runs "Echo".
If you just want to send a file to the command window, you can use Type instead, and not use the redirector.
type input.txt

How to read a file when i am redirecting the script to a text file in shell

I am executing a script and redirecting the output to text file using command sample.sh -base BUG2 1> output.txt 2>&1
now in the script i want to read the contents of the text file to grep some words.so how can we read that text file while the script is running.
If I get it well, you want to execute a script, and redirect its standard output into a file and on standard output: tee is what you are looking for.
This command redirect its standard input into a file and on its standard output (http://ss64.com/bash/tee.html)
sample.sh -base BUG2 | tee 'file.txt' | more_script
I'm not sure I understand your question correctly, but from what I can guess and assume, it appears that you're trying to read from, and write to the same file.
You will be able to do so, but you won't be able to rewind, as Bash can not seek.
Read more on redirections.
If you're trying to do something like this: cat file | sed s/foo/bar/ > file, i.e. Reading from a file and writing to it in the same pipeline.
That woul'd be impossible.
You cannot read from a file and write to it in the same pipeline. Depending on what your pipeline does, the file may be clobbered (to 0 bytes, or possibly to a number of bytes equal to the size of your operating system's pipeline buffer), or it may grow until it fills the available disk space, or reaches your operating system's file size limitation, or your quota, etc.
( Quoted from Bash Pitfalls 13 )

Why is the `of` operand not needed when using dd with a pipe?

I've come across this piece of code:
$ sudo dd if=/dev/sda | ssh username#10.1.1.5 \ "cat > /media/disk1/sda-image.img"
Shouldn't there be an of operand somewhere in this code? Also, what is the purpose of the \?
Any help will be much appreciated.
Like many old Unix utilities, dd (actually older than Unix) is designed so it can be combined with other commands using pipes. The of argument is only required if you want its output to be written to a file; otherwise the default behaviour of dd is to write the data to stdout. In this case, the output from the dd command is piped using ssh to the cat command running on a remote host.
The \ doesn't do anything when the complete command is contained in one line. It's usually used in shell scripts immediately before a line break to inform the shell that the rest of the command continues on the following line.

Bash shells script 101 - variable definition, pbcopy, etc

I am trying to create a (my first) bash script, but I need a little help. I have the following:
#!/bin/bash
echo "Write a LaTeX equation:"
read -e TeXFormula
URIEncoded = node -p "encodeURIComponent('$(sed "s/'/\\\'/g" <<<"$TeXFormula")')"
curl http://latex.codecogs.com/gif.latex?$URIEncoded -o /Users/casparjespersen/Desktop/notetex.gif | pbcopy
I want it to:
Require user input (LaTeX equation)
URIEncode user input (the top result in Google was using node.js, but anything will do..)
Perform a cURL call to this website converting equation to a GIF image
Copy the image to the placeholder, so I can paste it to a note taking app like OneNote, Word, etc.
My script is malfunctioning in the following:
URIEncoded is undefined, so there is something wrong with my variable definition.
When I copy using pbcopy the encrypted text content of the image is copied, and not the actual image. Is there a workaround for this? Otherwise, the script could automatically open the image and I could manually Cmd + C the content.
URIEncoded is undefined, so there is something wrong with my variable
definition.
The line should read
URIEncoded=$(node -p "encodeURIComponent('$(sed "s/'/\\\'/g" <<<"$TeXFormula")')")
without spaces around the = sign, and using the $() construct to actually perform the command, otherwise, the text of the command would be assigned to the variable.
When I copy using pbcopy the encrypted text content of the
image is copied, and not the actual image. Is there a workaround for
this? Otherwise, the script could automatically open the image and I
could manually Cmd + C the content.
pbcopy takes input from stdin but you are telling curl to write the output to a file rather than stdout. Try simply
curl http://latex.codecogs.com/gif.latex?$URIEncoded | pbcopy
or, for the second option you describe
curl http://latex.codecogs.com/gif.latex?$URIEncoded -o /Users/casparjespersen/Desktop/notetex.gif && open $_

Resources