Shell script which will create a text file - shell

I need to write a shell script that will do the following: will allow to create a text file from the keyboard and would find out the file type.
Could you help me? Thank you

to create a text file from keyboard input, you can use
cat > yourfile
which will send everything you type to yourfile, until you type ctrl+D on an empty line. or else, you can use any text editor. In all cases, the user will need to know how to terminate the input.
Then, if you want to identify the content of this file, you might suppose that the first line is a shebang (typically :
#!/bin/bash
and analyse it.

Related

How to write String to a text File

My question is about file handling in bash.
I want to write a string to text file in Bash script.
I have already tried
echo string>>filename.txt
your code is correct.you can write to a file using that.But you have to check the file access permission of the file you want to write the data in to.
Check following for more details.
Create text file and fill it using bash
Open and write data to text file using bash/shell scripting
That is a valid way of doing it. I would however consider this:
echo "string" >> file.txt
The quotes make it possible to handle more than one word (in the case of echo, that works anyway, but it's good practice to always use it). You should also distinguish between > and >>
> truncates the file (erases it) and replaces its contents with your string
>> on the other hand appends your string to the bottom of your file

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.

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 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.

I want to create a shell script that will create a pre-formatted file and open it in vim using a file name as an argument

I want to create a shell script that will create a pre-formatted file and open it in vim.
Specifically, I want to create a script called newperl that will allow me to type newperl [filename] and generate a file (using cat, echo, >, etc.) that is pre-formatted with
#!/usr/bin/perl
use 5.014;
etc.
I know how to do all the formatting etc. My problem is passing the [filename] which I type in after newperl to the rest of the script as an argument. How do I do that?
There is no need to write a shell script. Vim already has a hook that will allow you to read in a pre-existing file when you open a new file.
First, save your perl template somewhere convenient. I keep a directory ~/.vim/new_file_templates, containing skeleton.pl, skeleton.py, etc.
Next, add this to your .vimrc:
autocmd BufNewFile *.pl 0r ~/.vim/new_file_templates/skeleton.pl
This says that when you open a new file in vim whose name matches '*.pl', read in the contents
of skeleton.pl at the first line of the new file.
Then just typing
vim newperl.pl
will open a new file called 'newperl.pl' using 'skeleton.pl' as a template.
The variable $1 will automatically contain the word/quoted string passed after the call to the script.

Resources