bash open a vi and then execute a vi register - bash

I am currently trying to make a script that will take any number of arguments and create files for them. I have some registers saved in vi that I already have loaded with generic things I want in each file type.
So, I know that my shell can enter a file by typing vi "filename" but I am wondering if there is a way I could execute a register and then close the file, I suppose I could use my shell to append things I want to but it seems much more tedious which I am happy to do if you guys don't have any hints.
P.S. I have been looking online for way to open multiple tabs in the same terminal with only having generic Ubuntu without having to open a new terminal. But I think the only way to make this possibly happen is through programs I have to install, correct? Thank you for your time.

If you're trying to open files in an already-open execution of vim, then I think that'd have to be two processes:
outside process: to create the files (the one you planned to create)
inside process: (in your already opened vim) to load newly-appeared files (eg: like an automated :e new/file.txt)
wrt your PS: multiple files (buffers) is doable by default; a quickstart:
you can watch or read this screencast more about "buffers"
definitely shouldn't need new software
try this out yourself: vim ./foo ./bar (in a terminal)
:ls to see buffers you've opened (you'll see "foo" and "bar)
:buffer N to jump to one of said buffers
wrt "tabs" -- is that so that you can click? If so, maybe reconsider using vi (but if you're sticking to vi: you can find documentation to get tab-like UI if you really want, and again: no extra software).

Related

Octave- How to automatically execute commands on startup

I'm new to octave, and want to run a few commands on startup automatically every time it opens.
I typed "help startup" and saw "Octave uses the file ".octaverc". I did a bit of searching online at https://www.math.utah.edu/docs/info/octave_4.html, and saw the .octaverc file should be in the following path:
OCTAVE_HOME/lib/octave/VERSION/startup/octaverc
PROBLEM:
In that directory I don't have a startup folder, only "oct" and "site". I do see hidden files, which was my first thought since the file begins with "." character. So I then used Agent Ransack in the directory, and still nothing came up.
QUESTION:
1) Do I have to make the startup folder and octaverc file myself?
2) If so, does one, both or none have to be hidden?
3) Can it be a txt file, or does it have a special extension?
4) Do I just type the commands straight into the file or is there special formatting?
NOTE:
In case I'm going about this the wrong way, there are the operations I'd like to have run on startup:
PS1('>> '), addpath('D:\Users\Me\Desktop'), clc
Thanks ahead of time for the help!!
Possible locations (and their differences) for octaverc files are specified in the documentation.
In short, these are, from more general to specific:
octave-home/share/octave/site/m/startup/octaverc (most generic, for entire system)
octave-home/share/octave/version/m/startup/octaverc (to cover for more than one octave versions installed on the system, possibly requiring different startup scripts)
~/.octaverc (where ~ is unix-speak for a user's home directory -- covering for user-specific startup files)
.octaverc files in any directory, creating specific startup conditions for specific directories
octaverc files are effectively simple script files that are executed from most generic to most specific each time octave starts. Therefore, in the presence of conflicting commands, the more specific file can effectively be used to override the more generic behaviour.
Octave also supports (but does not recommend) the use of the startup.m file, for matlab compatibility.
You might also want to check out pathdef and savepath as well.
As a more general tip, if you ever want to search for a specific keyword from the documentation (e.g. octaverc), you can type this kind of search query in duckduckgo (or google):
octaverc site:https://octave.org/doc/interpreter/
(or just download the documentation as pdf and search the pdf)
Found the solution, the file was in the following path:
OCTAVE_HOME/share/octave/site/m/startup
to find out where OCTAVE_HOME is for you, just type "OCTAVE_HOME" into your Octave command line window.
ANSWERS:
1) You do not have to make a startup octaverc file yourself
2) The file is actually not hidden, so it should be easy to find given you're looking in the right place.
3) The file doesn't have an extension. It's just octaverc.
4) Under the last line of the existing file, you can just append commands as you would type them at the Octave command line window.
the last(7.3.0) octave version placed HERE:/ does not find the THERE:/openEMS/matlab directory even it is already loaded with octaverc or addpath. It keeps looking into the work dir where openEMS is not placed and does not recognize, for instance, the 'physical_constants.m' file.

How to sanitize an input command in shell script by blacklisting/whitelisting?

I'm working on a very expanded source code. I added a number of new source files to different paths, and wrote a shell script to open all of my files in a text editor.
I get an input from user to open files in his favourite code editor (gedit, geany, sublime, ...). For the sake of defensive programming, I would like to filter some commands like rm which can delete all my files instead of opening them!
My question is what is the best way to achieve this? If I want to blacklist or whitelist a set of commands, do I need to compare the input command against all of the commands in the blacklist/whitelist? Is there any shorter way?
If you want a user to be able to edit files but not run other, arbitrary commands on them, do all of the following:
Store the files under a user account the user doesn't have direct access to.
Configure /etc/sudoers to allow that user to run only specifically whitelisted commands as an account able to read the file.
Support only editors which have a restricted mode. By default, for instance, vim can be used to run arbitrary commands, but it has a (restricted) rvim alternative which doesn't allow privilege escalation.
Or do the sane thing, and use version control. :)

how do I run my .py file from within python command line?

background info:
I know this question has been asked countless times, but I'm not understanding the answers.
Today is the first time I've ever done anything with python, so I'm a total noob.
I'm using windows 7.
python 3.3.5 lives here: C:\Python33\python.exe
I have a .py file I want to run saved here: C:\Users\Scydmarc\Documents\cs188\python_basics\myfile.py
If I simply double click on myfile.py, it opens, runs, and closes super fast. By doing a perfectly timed printscreen, I can see that it works. It is my understanding that I need to run the file from within python.exe to get the window to stay open and allow me to mess with variables etc after it runs. So I open python.exe. What exactly do I need to type to run myfile.py? I've found lots of people trying to do this, but I guess I'm not understanding the answers. When I try to follow along, I get tangled up with errors. Do you still need to put python before the file name while in the python.exe window? Do I somehow need to specify a full path to the file? Some are actually running from the windows command prompt and not the python command prompt. When trying to change PYTHONPATH, are you supposed to do that from inside python, or the windows command prompt? Some examples use '/', while some use '\'. So, I think a complete example (what to actually type, in what window, line by line, with real filepaths) would clarify a lot for me and be super helpful. I'm thinking if I can actually see it done once, I can figure out how to make it do what I want in the future. Thanks for any help you can give.
in your python dir type python.exe pathtofile.py or set python.exe to your classpath
edit: https://www.youtube.com/watch?v=IokKz-LZsEo

run UNIX programs from the working directory?

In order to run UNIX programs from my current directory, I included this in my ~/.profile (I'm using Mac OS 10.9):
export PATH="./:$PATH"
Anything wrong with this approach? It seems obvious, so why isn't it included by default? And, is there any better way to do it?
Don't do it.
Adding . to $PATH is a security risk, that's why it's not like that by default. Consider the paths listed in $PATH as "trusted locations". You can run the programs and scripts in them without typing their absolute paths.
If you add . to $PATH, then you may run things by accident. For example I often run the netstat command to check statistics. I'm so used to it, I type only until "nets" and press tab, and I know it will be auto-completed to "netstat" so I very quickly press enter. If I had . on my $PATH, and there was a malicious script named "netst" in the current directory, then I might accidentally run it when I mean to run netstat as usual, pressing enter too fast to realize that tab auto-completed to netst instead of netstat.
This is just one example, I could easily think of more. Having . on $PATH is a security risk, that's why it's never there by default in any system. Appending . to the end is better than prepending it, but it's really best to not do it at all. Typing the ./ in front of programs should not be too much of a hassle, and you have the peace of mind of knowing exactly what you're running.
I think the conclusions in the duplicate questions are too soft:
https://superuser.com/questions/156582/why-is-not-in-the-path-by-default
https://unix.stackexchange.com/questions/65700/is-it-safe-to-add-to-my-path-how-come
Nobody should ever do this. The convenience this gives is ridiculously small compared to the dangers.
That's a good way to do it if you need it.
One reason it's not included by default is that malicious packages (of any kind, like tgz) can contains programs named the same as system commands that will remove things, start viruses or small daemons for future DDoS attacks.
What if a pack contains a program called emacs or vi? That besides showing the directory or change working directory also does something else?
So before putting that in your path consider what you will download.
Personally I'm happy with running ./local_program instead of putting it on the path.
The dangers of putting . in your $PATH are easy to find in other SO answers, so I'll just concentrate on "is there a better way to do [it]?". If "[it]" is "run my scripts without annoying punctuation," then the better way is to put all of your scripts in a directory called, for example, ~/bin, and add that to your $PATH.
You still need to be careful about what you put into that directory, and you will still have to avoid reusing names of built-ins as executables (test and time are popular bad names), but it doesn't open you up to random exploits or unexpected consequences of typing ls from the wrong directory.

How do I save my command snippets for quick access from the commandline?

I have a huge file with all the commands I use on and off the commandline. This file is getting harder to open and navigate as its size gets larger. I am looking for a commandline utility that makes this process easier. I found this gem, which does something similar to what I want, but it's terrible with long lines of code with multiple quotation marks.
What does everyone use to keep your code snippets and easily access it from your terminal?
I keep a similar file myself. This example isn't directly useful with your file. However when I find that I need to run a particular command I recently used, without looking it up again, I run a grep on history.
For example:
history | grep "svn co"
You may try Komandi, a multiplatform command snippets manager.

Resources