If you need to set up redirection with Apache, do not waste your time using the Rewrite module, instead use Redirect, the easy way:
1 2 3 4 5 6 7 8 9 | |
I find this pretty straightforward and more readable.
If you need to set up redirection with Apache, do not waste your time using the Rewrite module, instead use Redirect, the easy way:
1 2 3 4 5 6 7 8 9 | |
I find this pretty straightforward and more readable.
In this post I’ll explain how to rewrite your git commits and how to upload your private git repositories to github, but keeping your commit history. Recently I forgot to define my email address on the ‘.gitconfig’ file, this means all my history is dirty, and I wanted to fix it. Of course you can rewrite your commit history.
These are my last two commits on a repository with the wrong email address,
1 2 3 4 5 6 7 8 9 10 11 | |
Then I found git filter-branch(1) :
1
| |
It’s pretty straighforward, let’s check the new email address:
1 2 3 4 5 6 7 8 9 10 11 | |
As you might already know, the SHA-1 hash is different, now you can’t keep pushing to your remote if you ever had one. On the other hand, I have my respository almost ready to pushing into github. First create your respository in github, and the proceed with the next step:
1
| |
There are more properties you might want to define, check theme out.
I started making my own puppet modules and I want to share apt-sources. It’s a pretty straightforward module to manage debian repositories in a simple way. If you’d like getting started with puppet keep reading.
Basically the module allows to define one or more repositories, besides of the area. In Debian we find three areas, main,contrib and non-free, though the last two are not part of Debian. In any case, you could find different areas when using non-official repositories. The module itself only updates the /etc/apt/sources.list file, you do not forget to update your system afterwards.
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
And now we only need to define the template.
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
And some examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
This approach is quick and useful, and does the work. Just a bunch of ideas to improve it: Keyring management, Management of areas in apt-sources.d, and apt-sources as a puppet type.
Recently I ran into an issue regarding a vm’s storage. It turns out one of the VDI on my virtual machine was faulty. I had some data inside and I didn’t want to lose it.
First of all, we can convert from VDI to raw. I did the conversion from Windows, I guess it should be the same from Linux.
1
| |
Now let’s see what is inside the raw disk:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
In this case I only have a first partition, because the second one was used as swap device. I have to find out the offset where the data is.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
The ‘Start’ column shows me the offset for the partition I’m interested in. Next step is to map this offset with a loopback device and mount it.
1 2 3 4 5 6 7 | |
At this point you should be able to access your data, or at least some part of it.
After reading An introduction to bash completion I came up with an idea. I thought it would be really nice to have bash completion for all my git repositories. Furthermore I have a couple of functions that help me out to create new repos on the remote server and gitfy the current folder. For the next two functions I assume public key is enabled.
The function below takes a list of directories and creates bare respositories on the remote server.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
In order to keep a list of my repositories, every time I create a new repository I append it to my repolist. Sometimes I have been working on something and I want to add the current directory to a remote repository, here is another function that helps:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
Here is the bash completion for my git repositories :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
And the example :
1 2 3 4 5 | |
I’m quite sure it could be much better but for me works fine. It’s pretty handy, and I had the opportunity to play around with bash completion.
Today in the IRC somebody asked which would be the best way to know if a process already exists in the system. The choice was between using test -d /proc/PID or kill -0 PID.
Both of them do the job, the question here, we want to use the best one. Suddenly I remembered an option that comes with strace and lets you query the number of syscalls for a given trace. Besides we can order based on the number of syscalls.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
On the other hand, the second command’s output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
The column calls helps to know the number of performed syscalls. Using test -d /proc/PID gives a better performance due to a minor number of syscalls.
I really like strace, is a tool you had better know, here I got syscalls statistics, but you can trace syscalls either specific or a bunch of them, follow forked processes and much more, this is only a simple example. I hope it helps.
The first time I came up with this script was a long time ago and I did it with Perl. Today I feel like doing such a thing using Ruby.
Basically the script takes a number of directories and an optional pattern (last argument) from the command line. If either a directory or a pattern is not providen, it looks for in the current directory, and use a default pattern.
The script searches for those files matching the pattern and deletes thoses files from the system. Besides, it checks the owner of the files is the same that is running the script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
The default pattern is set to delete backup files ending with ”~”. Below is the ouput:
1 2 3 4 | |
You might change the pattern from the command line:
1 2 3 4 5 | |
This is how it works, I did not try too many different patterns beyond these.
Finally here is my last compilation about shell environment variables.
If this is set, keeps a directory list separated by ‘:’ and every time that you type the command “cd DIR” it searches for the directory in the above list, even if your current directory is not the right one. Let’s see an example, I define the directory holding all my git repositories, if I try to make cd repo from any part on the system, even if I not in the right place, I will get there:
1 2 3 4 5 | |
In my opinion is useful to keep just one directory or probably up to two, more than this, it could get messy.
If you want to narrow down the ouput when performing filename completion, this is without a doubt your shell env. When set, it ignores suffixes while performing filename completion.
1 2 3 4 5 | |
I mentioned this shell env because it was curious to me. I guess I have never found any case where I had to use such a thing, but that does not mean it wouldn’t be useful to somebody else.
It can be set to ignorespace , and it does not record words starting with empty space. On the other hand I do not like to have duplicated lines, ingnoredups does not repeat entries in the history. Finally, I like to join both options so I use ignoreboth instead.
1
| |
It holds the path to a file containing a list of hosts in the same format as /etc/hosts , if it is set, tries to complete the hostname with one of the entries on the file. Otherwiese will look for /etc/hosts.
It could be pretty useful if you want to keep a personal file with your hosts.
1
| |
The only problem I see here, if I you want to access hosts defined in /etc/hosts. You could make a script that checks if there is a new entry in the /etc/hosts and then appends the last entries to your personal list. Once again, this is only an idea I came up with, but actually I did not follow through.
If set, bash uses its value as the name of a directory in which creates temporary files. With this option I can set my personal temp directory.
1 2 | |
Sets a timeout and affects to read or select builtin commands, when not input is given. An interesting case is if you set this to your current shell. After N seconds without providing any input it will kill your shell, so keep this in mind.
1 2 3 4 5 6 7 8 9 | |
Well, the last posts were focused on bash shell environment because I was digging into the man bash and I found quite interesting things I did not know, I hope some of them were useful for you as well.
In the previous post I wrote about different topics such as BASH_ENV, subshells or expressions. Today I’m going to talk about shell enviroment variables.
This is the PID of the current bash process. This behaviour is different from $$ in cases such a subshell where $BASHPID says the PID of the subshell, whereas $$ shows the PID of the bash holding the subshell.
1 2 | |
Number of lines in the current script, from the beginning to the line the function was called from.
1 2 3 4 5 6 7 8 9 10 11 | |
Array with the directories you are moving using popd and pushd builtins to add/remove directories.
1 2 3 4 5 6 7 8 9 10 | |
EUID of the current user, initialized at shell startup. This variable is read only. On the other hand GROUPS is an array of groups which the current user is member of.
1 2 3 | |
Below there are some of the shell environment vars you could use, for instance if you need to check the architecture. I have not tried in Sparc yet but I would like.
1 2 3 4 | |
1 2 3 4 | |
In sum, there are a wide number of shell environment variables to keep in mind if you are scripting. Next time I will post more shell vars, but focused on customizing your bash shell.
One of the things I should do more often is to read the man pages. Recently I have spent some time digging into the bash man pages, and I would like to share some interesting stuff.
If you run a script, it looks for this variable and if it’s set, expands the value to the name of a file and reads its content. The filename should content the absolute pathmane otherwise it will no be able to locate the file.
1
| |
Take a look to the above example. From now, and beacuse I set BASH_ENV in my .bashrc, my scripts will have a set of fuctions or whatever I defined in there. It would be the same doing source $HOME/.customs in every script. This is really good, you have a set of custom functions or variables available to all your scripts.
A good idea would be to set this variable in either /etc/profile or /etc/bash.bashrc , if you want to share it with the rest of users in the system.
This is someting you might not pay attention. In order to assign the ouput of some shell commands to a variable, try first to use { }. Let’s see an example:
1 2 3 4 5 6 7 8 9 | |
Perhaps you realized the problem it crops up here.
Current shell {}
-Variables are available while the script is running. In the previus example I can access both variables, colors and line. Commands are separated by ‘;’. It’s also quite handy when using conditionals.
1
| |
Subshell $()
-Variable assigments do not remain. Any change to any variable in the script would not take effect after ending the command execution. However it would be possible to get the output by means of either echo or printf commands. Besides variable scope, performance could be worse due to new subshell execution.
You might know the old form [ ] , but this is the new one, and has some pretty cool properties as Pattern Matching. I guess the best way to get an idea is watching an example.
1 2 3 4 5 6 7 8 9 10 | |
Of course the above example is just for purpose, actually it does not validate a real ip address, but does the trick. Most interesting is the variable BASH_REMATCH, an array holding each substring matched by parenthesized subexpressions. Regarding regular expressions you should look for in the man as in regex(3) and regex(7).
This operator continues the execution to next option if available. I came up with an example and you will see how it works:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
Previus chunk of code is part of the pickcolor script. The main idea was to jazz some text up with a chosen color, then I thought it would be more practical to enable the option of pating the same text with all the available colors.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
The special operator ;& was really useful and did the trick. Next part I will write about shell vars and some builtin commands to have in mind.