VIM: chmod command is not an editor command - bash

I am trying to set permission for my file to be executed using the vim text editor. I have been using :chmod +x filename.sh but the error pops up that says it is not an editor command. I am even trying to set a path for the directory that my bash.exe and other files are, but I get a similar error. What can I do about this?

To run an external shell command, you need to use :!, not :.
:!chmod +x filename.sh
Also, you can use a % and it'll expand to the current filename:
:!chmod +x %

Related

How do I create new unix executable (.sh?) files for the terminal to use as commands?

I'd like to make some files to put in my /usr/bin folder (Mac OS) and be able to run them by typing the name to the terminal, like the commands gcc, cd, or vim (those are all in that folder). When I open such a file as text, it appears encrypted, so I'm not sure how to create one or what extension to use. Thank you.
The files you see in /usr/bin are not encrypted - they're compiled code in machine language different from bash scripts. You can however have scripts also in the /usr/bin location and have them run exactly as you are expecting.
In order to do that, you will have to create an executable script file. In unix, scripts are not identified by file extension but by 2 things:
executable bit set on the file permission level
Script interpreter header, such as #!/bin/bash
For a bash script, you can do the following:
Make a new file with the following content:
#!/bin/bash
echo "Hello world - My first bash script."
Save the file as hello.sh (the .sh is just convention, it could be any file name).
Then run chmod +x hello.sh and you will be able to run this file as an executable.
Move this file to /usr/local/bin and you should be able to run hello.sh from command line and it should execute your program.
You can create scripts, for example in bash, make the file executable with chmod and put the file path in your $PATH.
Example:
Create a new file called myscript.sh, you can use vi or any editor you prefer.
Add the content below on it:
#!/bin/bash
echo "Hello world!"
Now make it executable:
chmod u+x my script.sh
You can run your script like this ./myscript.sh or add it to your path:
export PATH=$PATH:$PWD
Now you can just run with the name, like myscript.sh
Programs such as gcc and cd are not encrypted, but compiled; however, if you create a shell script (without .sh as suffix; the .sh file extension for a shell script is optional) and place it in /usr/bin or any PATH location, and you chmod +x <script-path> (to give execute permission) then you can directly use that as a command.
Note: use shebang to mention the script interpreter, e.g. #!/usr/bin/env bash or equivalent for korn shell, python, etc.

How can I make my shell script executable

I wrote a shell script which opens some directories and runs some script, I run this bash file by terminal (bash filename.sh), how can I make it clickable?
You need to add the following shebang line to the top of your code.
#!/bin/bash
You also need to ensure that the script has executable permissions by running:
chmod a+x <filename>.sh
You first need to start your script with
'#!/bin/bash '
and save it as <filename>.sh Also make sure that you keep the permissions as a+x i.e all users can execute the script.

bash script "ignores" .vimrc

I have a problem as follows:
I have a script which copies a log file from remote machine, does some modification on it and then opens it in vim, problem is vim doesn't auto recognize the file type (which outside of the script id does) – I need this for coloring the log.
Script as follows:
/usr/bin/rcp 14.1.61.10$node:/output/LocalLog_IPNode$node.log /export/home/fpd/tmp/tmp_local_log
chmod 777 /export/home/fpd/tmp/tmp_local_log/*
sed -i 's/[A-Z]\{4,8\}.*[oigus][kbdct][sel]\//---/g' /export/home/fpd/tmp/tmp_local_log/LocalLog_IPNode$node.log
vi /export/home/fpd/tmp/tmp_local_log/LocalLog_IPNode$node.log
My .vimrc:
au BufNewFile,BufReadPost LocalLog* set filetype=local_log
Note that the files opens in vim (if it helps the manual command ":set syntax=local_log" doesn't work either).
After exiting the script and manually opening the log everything works fine =(
Your problem is that the autocommand option is only availaible in vim and not vi.
So if this is available on your system, you should replace the last command line by :
vim /export/home/fpd/tmp/tmp_local_log/LocalLog_IPNode$node.log
Vim stands for "Vi Improved" and many options are only available in the latter.
To be sure you can do:
:help autocommand
It is always mentioned if the feature is vi or vim compatible.

Converting a shell script to a dmg

I have a shell script with some functionalities. I want to convert it to an executable file. Any idea if this is possible?
Thanks
Add the following line at the very top of your script:
#!/bin/sh
This is known as a shebang. It indicates which program to invoke the shell script with when you execute it. You could change it to anything. Eg, to run a zsh script you would use #!/bin/zsh, which is the path to the zsh interpreter on my machine.
Then you need to mark the file as executable using chmod. You can do this as follows:
chmod +x myscript
You can now run the script like this:
/full/path/to/myscript
Or, if you're in the directory the script is in:
./myscript
The '.' expands to the path of your current working directory.
If you want to be able to run the script from anywhere, stick it somewhere in your path. Eg.
mv myscript /usr/bin
You can now run the script from anywhere by typing in just the name.

terminal sudo command

I'm a complete novice at this (terminal in Mac leopard) and hoping to get an lifeline from the web as I've certainly hit a wall.
I want to run a script as a root in terminal. The script is saved as a text file with a .rtf extension. I've inserted into terminal:
sudo filename.rtf
then I get asked a password which I've typed in (admin password) and pressed enter; then it shows a prompt: sudo: Mac: command not found
I've tried it with the extension hidden but got the same result. Any pointers on what I'm doing wrong?
Thanks in advance
You need to first get the script out of the .rtf file and into a plain text file (open it up in TextEdit and select "Make Plain Text" from the format menu, then save it again as myscript.sh).
Now you can type
sudo sh myscript.sh
The "magic" sh letters there are because as another responder says, sudo will temporarily elevate you to superuser and run a program. In *nix environments, that would be anything with the executable bit set, meaning that someone's explicitly told the operating system that it's safe to run a file. In your case, your myscript.sh has not been "blessed" in this way, so to run it you need to feed it into a program that knows how to understand it. That program is sh, and it does have the executable bit set. Thinking of it as sudo (sh myscript.sh) might make it a bit clearer.
If you plan on running this script a lot, you might want to actually make it executable on its own. This amounts to putting special instructions inside the file that tell the operating system how the file should be run. If you stick #!/bin/sh (this is called a shebang line and tells the OS what to do with your file) on the first line of your script, and then type chmod u+x myscript.sh (this tells the OS that you, and only you, are allowed to execute your file), you'll be able to run the file directly with sudo myscript.sh.
sudo is used to execute commands as the root user of the machine.
when you type
sudo [somthing]
the shell grants temporary root privilges and then executes the given "somthing"
assume your script is in bash, you should have done
sudo sh filename.rtf
Also, it's better to save script as plain txt, with an sh extension, so you would execute
sudo sh myscript.sh
first set the script as executable:
chmod +x filename.rtf
Then you can run it like so:
sudo ./filename.rtf

Resources