Posted by oxaric on December 4, 2008
Bash scripts are great! They make life a lot easier. But they can be a pain to drag around using mv and cp to get them to the right directory so you can execute them directly using: ~$ ./myscript.sh. Thankfully there is a much better way to run often used Bash scripts.
First, before we start you need to make sure the Bash script you’re interested in is executable. To be executable the script file needs the proper permission to run on your system. The command needed to give a file permission to run is chmod +x followed by the name of your script file.
This looks like:
| ~/test> chmod +x myscript.sh |
Once your script is executable you need to copy it to a directory that your system expects to contain executable scripts and code. On most systems you will have a choice between two directories. If you are the only user of your system you can cpy your script to either /usr/bin or /usr/local/bin. If you share your system with other people it’s best to copy your script to /usr/local/bin. You will most likely need super-user privileges to copy your script to either of these directories so you’ll most likely need to use the sudo command or an equivalent. The sudo command will give you temporary super-user privileges and allow you to copy the script. Now you can directly copy the script into one of the stated directories but it is my preference to remove the .sh from my script file names before copying them.
Here are two script file copy examples:
~/test> sudo cp myscript.sh /usr/bin/myscript
~/test> sudo cp myscript.sh /usr/local/bin |
Once you copy your script file it will be executable from any directory in your system. Simply type the name of your script file, hit enter, and your script will execute. Give it a try!
Posted in BASH, Linux, Tips | Tagged: bash, bin, executable, global, how, Linux, script, to | Leave a Comment »
Posted by oxaric on November 24, 2008
You may or may not be familiar with piping but if you use or plan to use a Linux machine you should definitely look into it.
Piping allows a command to send it’s output to another command. If you are familiar with programming you could think of this situation as calling a function, getting the output, and sending it directly to another function. Below are examples of two different forms of piping.
These are the commands I’ll use in the examples:
ls – lists all of the files in the current directory, Windows users might know this as the
dir command
find – when used with no parameters it will list every file inside the current directory including files inside of other directories
rm – deletes a file, Windows users might know this is as the
del command
grep – searches a string looking for a given string or pattern
Piping Example
What happens:
First, the ls command will find and try to display all the files in the current directory. However, the bar symbol | causes the ls output to be piped to the grep command. The grep command will then take the input from the pipe and search the piped lines for any string that contains “a.out“. If grep finds any such strings it will then directly display them.
Using the bar symbol, |, is the most common form of piping. But another form of piping exists called substitution piping.
Substitution piping can be used with commands that do not work with the direct piping method shown above. An example of such a command is rm which will not accept a direct piping of file names or directories to delete. The rm command only accepts file names and directories if they are directly given in it’s parameters while calling it. This is the beauty of substitution piping. It allows you to perform a command and send the output line-by-line as a parameter to another command.
Substitution Piping Example
| ~/test> rm $(find | grep a.out) |
What happens:
First, the commands inside of $() will run and produce a list of all files in the current directory and in any directories inside the current directory that have “a.out” in their file name. As you can see, both forms of piping can be used together. Once the commands inside the substitution pipe are completed the output will be passed line-by-line as a parameter to the first command rm which will delete every file passed to it.
Caveat emptor!
Be careful!
Piping can be a force for good and evil. It is extremely easy to be mistaken about the exact output of a command and end up deleting precious files or doing all sorts of crazy things. Test all of your commands before piping.
And remember, a wise old man once said, “To use commands is human, to pipe divine.” ;)
Posted in Linux, Tips | Tagged: bash, beginner, command, example, Linux, piping, substitution | 2 Comments »
Posted by oxaric on November 17, 2008
If you use bash then right now you can create aliases. An alias allows you to define a command that will call another command that is much longer or doesn’t automatically have the options you want.
If you want to list all of the files in your current directory you would use the command
"ls". However, this command by itself will not show you hidden files. In Linux any file that starts with a period “.” is considered a hidden file. If you want to see all the files in your current directory including normal and hidden files you would need to use the command
"ls -A".
Typing
"ls -A" a few times isn’t such a big deal but if you are constantly using this command it would be quicker to create a bash alias. To create a bash alias we use the command “alias”. Makes sense huh? :)
Here is the basic format for the alias command:
alias new_command_name='old_command -plus_the_options_you_want'
NOTE: The new command name does not have any quotes around it but the old command with the added options has single quotes around it.
To make an alias for
"ls -A" you need to type the following in your bash shell:
| ~/test> ls alias ls=’ls -A’ grep a.out |
Once you’ve typed this line hit enter and the alias is created. Now during your current bash session if you want to see both the hidden and normal files in your current directory you only need to type "ls".
But wait!
If you create an alias manually like above you will lose all your aliases when you close the bash shell. If you want these aliases to be a permanent feature of your bash shell you need to do two things.
- Add the following line to your "~/.bashrc" file:
if [ -f ~/my_bash_additions ]; then
. ~/my_bash_additions
fi |
NOTE: To edit this file you will need to be root or use "sudo".
-
Create a file in ~/ called “my_bash_additions” and put your alias commands into this file.
Once you’ve done this all that’s left is to restart your bash shell. All of your aliases will be saved and ready for use. If you typed any commands wrong or anything is amiss you will probably get an error message when your bash shell is loading. If you get any errors go back into your “my_bash_additions” file, fix the errors, and re-start your bash shell. But I’m sure you’ll never make any mistakes so you’ll never have to do this. :)
Extra
Another cool thing you can do with your bash shell is to edit the way your command prompt looks. You can make your prompt colorful, plain, add extra information such as the current time, lots of stuff.
Here is a site with good information about editing your command prompt.
I’ve uploaded my own “my_bash_additions” file. Feel free to download it and try it out. It has a lot of features I like. It doesn’t have too many comments but it shouldn’t be too hard to understand.
Click to directly download my_bash_additions
Have fun playing!
Posted in BASH, Linux, Tips | Tagged: alias, bash, color, command, edit, Linux, prompt, script | Leave a Comment »
Posted by oxaric on November 12, 2008
Occasionally you might find you’ve created ‘bad’ file names. These file names start with a hyphen and are an annoyance and possibly dangerous to your system. However, if you try to directly use ‘rm -file-name’ you’ll find it won’t work. The solution to this problem is simple once you know how but can be very confusing if you don’t.
The ‘trick’ is to add “./” to the beginning of the file name before trying to rm the file. If the file name contains spaces you’ll also need to wrap the file name in double quotes.
For example:
~/test> ls
important-system-information
~/test> echo “This is a test” > -rf
~/test> ls
-rf
important-system-information
~/test> rm -rf
~/test> ls
-rf
important-system-information
~/test> rm “-rf“
~/test> ls
-rf
important-system-information
~/test> rm “-rf” important-system-information/
~/test> ls
-rf
~/test> rm ./-rf
~/test> ls
~/test> |
***POOF!***
The bad file name is gone! But unfortunately in this example we deleted our important system information directory by trying to simply wrap the bad file name in double quotes. The rm command took ‘”-rf” as an option, which forcefully and recursively deletes, and we happened to type the name of our important system directory on the same line as the rm command. OOPS! Contrived example? Yes. But it works. :)
Posted in Linux, Tips | Tagged: bad, dangerous, file, hyphen, Linux, names, trick | Leave a Comment »