Can you execute a shell command from that file by reading it? - shell

Given
unix
file with regular text and a unix command , and other regular text
-
Is there a way to write that command so that when we do a less or a cat (simple reading) or some innocent operation on that file, we actually trigger the command?

Related

Modify textblock by external script in vim [duplicate]

This question already has answers here:
Replace vim selection with output of shell command
(4 answers)
Closed 1 year ago.
While editing files I often encounter the following task: I have a standalone script that takes input and outputs a modified version of it (could be sort, could be a sed script, could be a more complicated python script) and I want to run it on a block of text within vim.
This answer explains how to feed a text block to an external script and I assume that one can similarly read a script's output into a file using :r. But how can I do both: feed text out of vim to an external script and its output back into vim?
You can filter a block of text in vim using the normal mode commant
!{motion}. For instance, to use the external sort tool on the text below
(ignoring the fact that vim has it's own sort for now):
b
a
d
c
use !ip (external command !, inside paragraph), which will take you to
the command line with a prepopulated command line prompt (mine looked like
:.,.+4!) where you simply type whatever external tool (e.g. :.,.+4!sort)
you want to send the text to as stdin. The resulting stout will replace the
selected lines:
a
b
c
d

How to write list of executing program in a text file by bash

I trying write a program with bash file that create a text file then write the List of running programs file in the text file. I created a text file with bash that name is test.txt, how can I write List of running programs in the text file?
I put my code here:
#!/bin/bash
cat test.txt
To get a list of running programs, use ps. Run man ps for details.
To redirect the output of a command to a file, use >.
ps > test.txt
See REDIRECTION in man bash for details.

What does the redirection operator "<" do in shell scripts?

I came across the below shell command:
$prog.sh < file_name.json
I know it reads from a file, but how and where does prog.sh load the file?
Every program has three open file handles at startup, one of which is standard input. Normally, the file handles are inherited from the parent process. The < operator tells the shell that, instead of passing its standard input to prog.sh, to open file_name.json instead and give that file handle to prog.sh as its standard input.
$prog.sh < file_name.json
As you rightly guess. The < is meant for redirecting the input from a file so that your script will read from the file which will be the (temporary) stdin(fd0).
it read from a file, but how and where prog.sh will load the file
It depends on how you plan to go about it. Any command in the script that expects an input from the stdin will now read from the file. The new line character in the text file (usually) stands for the ↵ in the stdin.

edit/save a file within shell script

I think this question fall under pipes, am bad at it.
Using one of my shell script, a file is generated with millions of rows.
Before I can use it with another command, I need to edit this file. I need to add a text e.g 'txt' in front of every line.
What i am currently doing now is,
-exit the shell script after file is generated
-open it in vim
-use command :g/^/s//txt/g to add txt at start of each line
-save file
-use it in remaining shell script
I am sure there would be a more efficient way, which i am not aware of. thanks for the help.
As some people said in the comments, you can use GNU sed to do that:
sed -i 's/^/txt/' yourfile.txt
The -i stands for --in-place and edit your file instead of printing to stdout.

How to execute command inside vim?

I want to run shell script inside Vim editor.
I heard it is possible but do not know.
Command:./shell.sh inside vim.
There are multiple ways to do it. A primary question is "Do you want the output from the script in the file?"
If you want the output in the file:
:r!./shell.sh
If you don't want the output in the file:
:!./shell.sh
If you have the line ./shell.sh in the file, you can include the output in the file with:
!!sh
If you've done it before, you have more options.
If you save the command in a named buffer you have still more options.
If you want the script to have a portion of the file (edit buffer) as its standard input, you have an enormous number of options you can use in conjunction with either of these mechanisms.
Prefix the command with a !. For example, open Vim and write:
:!ls
This will execute the shell ls command.
Note that you'll have to be in the correct directory within Vim for this to work.

Resources