• Home
  • Shell
    • Emacs
    • Perl
    • screen
    • sed
  • Ubuntu
    • VNC
  • Web Development
    • Javascript
    • Joomla
    • MySQL
    • osTicket
  • Windows
    • Gimp

Posts in category BASH

BASH> Colorize your ‘cat’ output

Apr07
2011
Written by Scott Rowley

Ok, well thats a bit misleading. We won’t actually be using ‘cat’ but ‘pygmentize’ and its not perfect, but its better than 1 single color

This runs on Debian/Gentoo based Linux systems

apt-get update
apt-get install pygmentize

Once thats been installed what I did was to make an alias in my .profile

alias pcat='pygmentize'

Make sure to reload your .profile if you’ve added your alias

cd ~
. .profile

Example cat and pcat outputs:
Cat OutputPygmentize Output

Tagged alias, cat, pcat, pygmentize, ubuntu

BASH> Check for last day of the month

Apr01
2011
Written by Scott Rowley

Script is meant to be used with other scripts in order to determine if it is the last day of the month. Useful for making sure a particular script runs at the end of the month. Just add a cron job that checks this every day at the time you would want your code run if it was indeed the last day of the month.

2 0 * * * /usr/bin/last-day-of-the-month.sh
#!/usr/bin/bash
# last-day-of-the-month.sh
# By: Scott Rowley
# http://www.sudobash.net/
#########################################
TODAY=`/usr/local/bin/date +%d`
TOMORROW=`/usr/local/bin/date +%d -d "1 day"`
# See if tomorrow's day is less than today's
if [ $TOMORROW -lt $TODAY ]
then
echo "Today is the last day of the month, running code…"
/run/your/code
fi
exit 1

BASH> sudo bash

Mar31
2011
Written by Scott Rowley

*Note — This is not an original sudobash.net article — credit goes completely to
http://www.gratisoft.us/sudo/man/1.8.0/sudo.man.html. But I know people use search engines on ‘sudo bash’ and end up here so why not provide the answer as well? 😉

sudo, sudoedit — execute a command as another user

SYNOPSIS

sudo [-D level] -h | -K | -k | -V
sudo -v [-AknS] [-a auth_type] [-D level] [-g group name|#gid] [-p prompt] [-u user name|#uid]
sudo -l[l] [-AknS] [-a auth_type] [-D level] [-g group name|#gid] [-p prompt] [-U user name] [-u user name|#uid] [command]
sudo [-AbEHnPS] [-a auth_type] [-C fd] [-D level] [-c class|-] [-g group name|#gid] [-p prompt] [-r role] [-t type] [-u user name|#uid] [VAR=value] [-i | -s] [command]
sudoedit [-AnS] [-a auth_type] [-C fd] [-c class|-] [-D level] [-g group name|#gid] [-p prompt] [-u user name|#uid] file …

DESCRIPTION
sudo allows a permitted user to execute a command as the superuser or another user, as specified by the security policy. The real and effective uid and gid are set to match those of the target user, as specified in the password database, and the group vector is initialized based on the group database (unless the -P option was specified).

sudo supports a plugin architecture for security policies and input/output logging. Third parties can develop and distribute their own policy and I/O logging modules to work seemlessly with the sudo front end. The default security policy is sudoers, which is configured via the file /etc/sudoers, or via LDAP. See the PLUGINS section for more information.

The security policy determines what privileges, if any, a user has to run sudo. The policy may require that users authenticate themselves with a password or another authentication mechanism. If authentication is required, sudo will exit if the user’s password is not entered within a configurable time limit. This limit is policy-specific; the default password prompt timeout for the sudoers security policy is 5 minutes.

Security policies may support credential caching to allow the user to run sudo again for a period of time without requiring authentication. The sudoers policy caches credentials for 5 minutes, unless overridden in sudoers(5). By running sudo with the -v option, a user can update the cached credentials without running a command.

When invoked as sudoedit, the -e option (described below), is implied.

Security policies may log successful and failed attempts to use sudo. If an I/O plugin is configured, the running command’s input and output may be logged as well.
READ MORE »

Web Dev> Report on server script status using popen

Mar24
2011
Written by Scott Rowley

In your PHP page add something like the following:

$variable = popen("path/to/script.sh", 'r');
while (!feof($variable)) {
echo fgets($variable);
flush();
ob_flush();
}
pclose($variable);

Then in your script you can echo the output like normal — though I recommend modifying it to spit out HTML like so:

while [ 1 ]
do
read CURRENT_ACCOUNT || break
echo "Working with account $CURRENT_ACCOUNT…<br />";
done < list_of_accounts.txt

Then when you run the php page your webpage will be updated each time with the output of your script.sh until it is complete, then it will resume with the rest of your php code.

Posted in Web Development

BASH> ‘sed’ command examples.

Nov15
2010
Written by Scott Rowley

Another list that will grow as I need it…

Remove blank lines from output

sed '/^$/d'

Replace data with new data

sed 's/DATA/NEWDATA/g'

Replace multiple spaces with single space

sed 's/ */ /g'

Replace first instance of string

sed -e 's/pattern/REPLACEMENT/1'
Posted in sed - Tagged blank, data, empty, output, replace, sed

BASH> ‘find’ examples

Nov12
2010
Written by Scott Rowley

This will be a growing list as I find commands I need…

Find files of .ext that are 0 bytes and rm them.

find . -name "*.ext" -size 0 -exec rm {} \;
Tagged byte, example, find, rm, shell

BASH> Variable checking

Nov11
2010
Written by Scott Rowley

Check the variable is a whole number

if [ $VARIABLE -eq $VARIABLE 2> /dev/null ]; then
echo $VARIABLE is a number
else
echo $VARIABLE is not a number
fi

Check the variable is not empty

if [ $VARIABLE != "" ] ; then
<code_to_execute>
else
echo "Error: VARIABLE is empty"
fi

or as provided by solstice:

if [ -z "$VARIABLE" ] ; then
echo “VARIABLE is empty”
else
echo “VARIABLE is not empty”
fi

or -n to test if it”s not null

if [ -n "$VARIABLE" ] ; then
echo “VARIABLE is not empty”
else
echo “VARIABLE is empty”
fi

Check the variable is alpha only

if [[ "$VARIABLE" =~ ^[a-zA-Z]+$ ]] ; then
echo "Variable contains letters only"
else
echo "Error"
fi

BASH> Connect to another user’s console terminal using ‘screen’

Nov02
2010
Written by Scott Rowley

Recently, I was helping another Admin and I wanted to be able to share our screens but our IS department won’t allow for it. Being that we were working in a terminal session I decided to go this route instead.

Needed:
– Screen
– Local account on host computer/server for remote user

Install screen

sudo apt-get install screen

Set the screen binary (/usr/bin/screen) setuid root. By default, screen is installed with the setuid bit turned off, as this is a potential security hole.

sudo chmod +s /usr/bin/screen
sudo chmod 755 /var/run/screen

The host starts screen in a local xterm, using the command screen -S SessionName. The -S switch gives the session a name, which makes multiple screen sessions easier to manage.

screen -S screen-test

The remote user (remote_user) uses SSH to connect to the host computer (host_user).

ssh remote_user@server

The host (host_user) then has to allow multiuser access in the screen session via the command ^A :multiuser on (all ‘screen’ commands start with the screen escape sequence, ^A).

^A
:multiuser on

The host (host_user) must grant permission to the remote user (remote_user) to access the screen session using the command ^A :acladd user_name where user_name is the remote user’s login ID (remote_user).

^A
:acladd remote_user

The remote user can now connect to the hosts ‘screen’ session. The syntax to connect to another user’s screen session is screen -x host_user/sessionname.

screen -x host_user/screen-test
Posted in screen - Tagged connect, screen, share, terminal, user

BASH> How to install perl modules through CPAN on Ubuntu

Oct26
2010
Written by Scott Rowley

Install all dependent packages for CPAN

sudo apt-get install build-essential

Invoke the cpan command as a normal user

cpan

Once you hit on enter for “cpan” to execute, you be asked of some few questions. To make it simple for yourself, answer “no” for the first question so that the latter ones will be done for you automatically.

Enter the commands below

make install
install Bundle::CPAN

Now all is set and you can install any perl module you want. examples of what installed below:

cpan prompt> install IO::File
cpan prompt> install Net::SMTP_auth
cpan prompt> install Email::MIME::Attachment::Stripper
cpan prompt> install Mail::POP3Client
Tagged cpan, install, module, Perl

BASH> Remove tabs from output

Oct26
2010
Written by Scott Rowley

In order to remove tabs from your output (say to get a specific variable) use the ‘tr’ command, for example

In my zone.file the line containing “Serial” looks like the following:
If I performed

grep Serial zone.file

I would get the following output

 2009110904 ; Serial (yyyymmdd##)

You can’t see them here but there are several tabs before that date. In my example I want to get just the actual serial number (2009110904). To do this I’ll first strip off the “; Serial…” information;

grep Serial zone.file | awk -F; {'print $1'}

This command is telling it to look for the ; special character and break up the output there, then from there it is told to print the first column of information this results in:

 2009110904

The problem here is that if we want this to be our variable we are actually working with ” 2009110904″ instead of the desired “2009110904” In this particular case we know those are tabs not spaces. So we’ll need to use tr in order to remove those tabs:

grep Serial zone.file | awk -F\; {'print $1'} | tr -d 't'

This will return to us exactly what we want

2009110904
Tagged awk, grep, tab, tr
« Older Entries Newer Entries »

Corrections? Questions? Comments?

Find an error?
Everything work out great for you?
Have some feedback?
Like to see something added to the article?

PLEASE leave us a comment after the article and let us know how we are doing, or if something needs corrected, improved or clarified.

Thank you!
- The Management

Advertisement

Sudo Bash
By Geeks - For Geeks

Back to Top