Problem with including an "image" in shell program - shell

I'm writing a program where at some point in my loop I want to print to output whatever is stored in a separate file (an image). But if I code it like this:
for c in $LIST
do
clear
./image.0
done
And the "image.0" file contains only an image like this:
+----+
| |
|
|
|
|
|
========
Then when I run my program I get this message:
./image.0: 1: +----+: not found
./image.0: 2: Syntax error: "|" unexpected
Why?
================================
So "cat" works, the image appears in the output but it's shifted in a strange way.
Do you know why this would happen?
+----+
| |
|
|
|
|
|
========
Answer: I put printf "\n" that fixed the shifting image

With ./image.0, you tell the shell to execute the image. You want to output it, so use cat image.0

Try to use the command cat to output the content of the image.0 file
cat ./image.0

./something will take something as a program and execute it. That's not what you want : to display the contents of a file, you can use the cat command, like this :
for c in $LIST
do
clear
cat image.0
done

Related

How to get the same output using Excel-VBA code?

I am working with an excel file, it only has one column as follows:
Type
A:\AAA\AD\RER\TES\11111\&DD&MM&AA.EXT
C:\AAA\CD\RES\TES\33333\&DD&MM&AA.EXT
C:\CCC\DF\WSD\&DD&MM&AA&SQ2.TXT
C:\DDDD\RT\FDG\334455&DD&MM&AA&SQ2.TXT
C:\DDD\YU\DFS\55555&DD&MM&AA&SQ2.TXT
C:\RRR\ER\SDF\55555&DD&MM&AA&SQ2.TXT
C:\TTT\CD\ERW\55555&DD&MM&AA&SQ2.TXT
C:\YYY\YU\WET\555555&DD&MM&AA.EXT
I would like to extract the following output:
&DD&MM&AA.EXT
&DD&MM&AA.EXT
&DD&MM&AA&.TXT
334455&DD&MM&AA&.TXT
55555&DD&MM&AA&.TXT
55555&DD&MM&AA&.TXT
55555&DD&MM&AA&.TXT
555555&DD&MM&AA.EXT
The approach that I followed to extract it was using bash since I am a beginner in the usage of excel, my command was the following:
rev colum.txt | tr -d " " | cut -d "\\" -f1 | rev | sed "s/SQ2//"
The problem with this is that I would like to achieve the same result using a macros of excel, I don't know how to program it, I would like to appreciate a suggestion of how to transform this bash code to Excel-VBA, supposing that the column that contains the data is the column A.
With data in column A, in B1 enter:
=MID(A1,FIND(CHAR(1),SUBSTITUTE(A1,"\",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))+1,9999)
and copy down.

cat command not displaying file contents with begin with + sign

I have created a file with contents as given below.
+---------------------------+
| Tasks
+---------------------------+
| 1) option one
| 2) option two
+---------------------------+
But it is not displaying anything while I am trying to run the following command
cat < filename
Please help me to solve this.
Thanks in advance.
Use cat filename. The redirection isn't necessary.
(You could do cat - < filename also).

gnu watch: justify on the lower left of the terminal

I want to apply a watch command on a mysql query every N seconds, but would like to have the results on the bottom left of the terminal instead of the top left:
watch -n 120 "mysql_query" | column -t"
Shows my results like so:
--------------------------
|xxxxxxxxxxx |
|xxxxxxxxxxx |
|xxxxxxxxxxx |
| |
| |
--------------------------
Whereas I would like them to have like so:
--------------------------
| |
| |
|xxxxxxxxxxx |
|xxxxxxxxxxx |
|xxxxxxxxxxx |
--------------------------
Suggestion?
I don't see a straight-forward way to do this, but I managed to force it to work using the following approach. I haven't fully tested this so I cannot guarantee that this will work in all situations.
Using this script:
#!/bin/bash
TERM_HEIGHT=`tput lines` # determine terminal height
WATCH_BANNER_HEIGHT=2 # account for the lines taken up by the header of "watch"
let VIS_LINES="TERM_HEIGHT - WATCH_BANNER_HEIGHT" # height of visible area
(yes " " | head -n $VIS_LINES; cat | head -n $VIS_LINES) | tail -n $VIS_LINES
Post process the output of your command as it is called by watch e.g. (assuming the script was saved as align_bottom, made executable, and store somewhere within your $PATH):
watch -n 120 "mysql_query | column -t | align_bottom"
What the script does:
Determine the height (number of lines) of the terminal
Calculate the visible area of the watch output
Print blank lines to pad the output (pushing the output down)
Read in output from stdin, and trim it so we only show the top of the output if it extends beyond the screen. If you want to see the bottom of the output instead, simple remove the head command after cat.
tail the output of steps (3) and (4) so excess padding is removed and the final output fits snugly within watch
I have to admit this seems a little hackish, but hopefully it gets you closer to what you're trying to achieve.
Update:
It should also be possible to implement that as a function instead just so it can sit comfortably in .bashrc.
function align_bottom() {
(( VIS = $(tput lines) - 2 )) # height of visible area
(yes " " | head -n $VIS; cat | head -n $VIS) | tail -n $VIS
}
typeset -fx align_bottom # !! make it callable from subshell
Usage would be the same:
watch -n 120 "mysql_query | column -t | align_bottom"
Note that watch runs the given command using sh -c, therefore, as Dennis pointed out in the comments, on systems that does not link /bin/sh to /bin/bash the function approach shown above will not work.
It is possible to make it work usign:
watch -n 120 "mysql_query | column -t | bash -c align_bottom"
but for portability and usability, it's cleaner to simply use the shell script approach.
I don't know if watch can do that, but what I'd do is use another tool to have multiple terminals and resize the one in which watch is running according to my needs.
A couple of these tools that can be useful are:
screen
byobu (screen with some enhancements)
terminator
I hope this helps.

this one-line shell script not working on Mac - any ideas how to fix?

This unix command I haven't got quite working on Mac yet - any ideas what needs adjusting:
find . | grep '.*\(css\|js\|rjs\|rhtml\|rb\)$' | sort | while read in; do printf "\n\n####\n# FILE: %s\n####\n\n" ${in} >> onebigfile; cat "${in}" >> onebigfile; done
thanks
The purpose of this command is to gather the content of all the files under the current directory whose names ends as said (css ... rb) in a file named onebigfile (with delimiters) IIUC.
To debug this type of series of piped commands, you can run the individual commands, or individual groups of commands to try to see what is happening. For instance, try:
find .
find . | grep '.*\(css\|js\|rjs\|rhtml\|rb\)$'
find . | grep '.*\(css\|js\|rjs\|rhtml\|rb\)$' | sort
Then get one line of the output (for example ./dir/file.css), and try:
echo './dir/file.css' | while read in; do echo ${in}; done
echo './dir/file.css' | while read in; do cat ${in}; done
echo './dir/file.css' | while read in; do cat ${in} >> onebigfile; done
You should bo able then to understand what's happening.
The problem may be due to file and directory names containing spaces. The solution in this case is to use find -print0 command.

How do I append onto pipes?

So my question is if I can somehow send data to my program and then send the same data AND its result to another program without having to create a temporary file (in my case ouputdata.txt).
Preferably using linux pipes/bash.
I currently do the following:
cat inputdata.txt | ./MyProg > outputdata.txt
cat inputdata.txt outputdata.txt | ./MyProg2
Here is another way, which can be extended to put the output of two programs together:
( Prog1; Prog2; Prog3; ... ) | ProgN
That at least works in Bash.
Choice 1 - fix MyProg to write the merged output from the input and it's own output. Then you can do this.
./MyProg <inputdata.txt | ./MyProg2
Choice 2 - If you can't fix MyProg to write both input and output, you need to merge.
./MyProg <inputdata.txt | cat inputdata.txt - | ./MyProg2

Resources