Edit: I should have pointed out originally, as I have now received feedback on this. This is NOT the best or optimal way of performing this task. I was trying to illustrate as many shell scripting principles as possible in terms of ‘if’, ‘for’, counters, etc, and how such a one liner has been put together. Perhaps I should have thought of a better way of illustrating such principles, but nevertheless, here it is!
Here’s a quick one liner, can’t think why anyone would ever have any use for it, but maybe the principle itself could be of use to someone! This will take a file containing listings of 16 digit numbers, i.e. 1234123412341234 and replace it with XXXXXXXXXXXX1234
Duly spaced and indented:
P=”"
ctr=0;
for I in `echo $I|grep -o .`; do
let ctr=$ctr+1;
if [ $ctr -gt 12 ]; then
P=${P}${I};
else
P=${P}”X”;
fi;
done;
echo $P|tr -d ‘\n’;
echo -ne “\n”;
done
Would love anyone to comment with variations.
Tags: bash, echo, grep, script, tr
BASH – The Bourne Again Shell amongst most if not all other shells allows each application to exit with a return code. Some shells and environments have limits on what range this integer can fall into. Something between 0 and 255 inclusive is always a safe bet. In BASH, the variable $? is populated with the return code of the last command to return control back to the shell. It is important to preserve the return code immediately after the application exits that we want to monitor, as subsequent commands will overwrite the variable. The ping tool returns 0 on success:
Now of course there are easier ways of achieving the above task, although I’ve laid out the script in this way hoping that the way I have laid it out illustrates capturing the code and preserving it beyond the ‘if’ that follows which would have overwritten it. Just as further illustration, calling ping invalid followed directly by echo $? shows a return code of ‘2′ – obviously the return code for such a failure. Calling echo $? again immediately after shows a return code of ‘0′ as the return code of ping was overwritten by the return code of the first echo statement. Bash builtins return codes to the shell as any other application would.
Tags: bash, host, monitor, ping, return code, script, shell, shell script, uptime, variable
I was asked today how to sort a string alphabetically with BASH
Using perl, you can easily enough use
print (join “”, sort split //,$_)
With bash however, the best option is:
echo “teststring” | grep -o . | sort -n |tr -d ‘\n’; echo
Which returns: eginrssttt
A good way of enumerating each character from a string in general is:
for (( i = 0; i < ${#str[@]}; i++ )); do echo “${str[$i]}”; done
Tags: bash, do, done, echo, for, grep, perl, print, shell, shell scripting, sort, string, tr
By default your history is stored under bash for 500 commands. You can view your history by issuing the command:
history
This is one good reason why you should not enter passwords as part of a command line where possible. i.e. use mysql -u root -p and then enter the password when prompted by the mysql command, rather than using mysql -u root –password=secretpassword
On login you can use unset HISTFILE which will cease logging to your history file.
You could also use rm -f ~/.bash_history; ln -s /dev/null ~/.bash_history
This will remove your bash history file, then link a file of the same name to /dev/null which is where your history logs will now end up!
Tags: bash, bash_history, command history, history, Linux, MySQL