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

Posts tagged function

BASH> Functions

Mar22
2013
Written by Scott Rowley

The beginning of a list of any functions I have created or found and deem useful:

This function will allow you to type “up ANY_NUMBER” and move that many directories up in the current directory tree you are in. If you you do not specify a number and instead just type ‘up’ by itself you will automatically go up just one directory.

function up ()
{
if (( $#> 0 )); then
COUNTER=$1;
else
COUNTER=1;
fi;
while (( ${COUNTER}> 0 )); do
UP="${UP}../";
(( COUNTER=${COUNTER}-1 ));
done;
echo "cd $UP";
cd $UP;
UP=''
}

Some servers don’t have ‘watch’ installed so I’ve made my own function to replicate it’s basic functionality (updated to work with pipes!)

function mywatch () {
while true
do
clear
date +'%D %r'
echo ''
cmd=`echo $@`
#echo "Command is '$cmd'"
last=`echo $@ | awk {'print $NF'}`
num='^[0-9]+$'
if [[ $last =~ $num ]]; then
cmd=`echo $cmd | sed "s^$last^^g"`
eval "$cmd"
sleep $last
else
eval "$cmd"
sleep 2
fi
clear
done
}

Useage:

mywatch "ls -al" 5

If a second parameter ($2) is not supplied (in this case 5) then 2 will be used and your watch will refresh every 2 seconds.

Easily locate problem directories when space is low on a server. Add the following function to your profile (.profile / .bashrc / etc)

function spaceHog ()
{
du -k | sort -n | awk '
BEGIN {
split("KB,MB,GB,TB", Units, ",");
}
{
u = 1;
while ($1>= 1024) {
$1 = $1 / 1024;
u += 1;
}
$1 = sprintf("%.1f %s", $1, Units[u]);
print $0;
}'
}

Useage:

[17:25:51][user@server][ ~ ]
[$]> spaceHog | tail -10 # To show the 10 largest, change this number as desired.
247.5 MB ./bin
247.8 MB ./.cache/google-chrome/Default
247.8 MB ./.cache/google-chrome
283.4 MB ./.cache
293.2 MB ./.wine/drive_c/Program Files (x86)
423.5 MB ./Downloads
653.6 MB ./IOS_APPS
655.1 MB ./.wine/drive_c
658.2 MB ./.wine
2.7 GB .

Delete (rm) a file by it’s inode number

Occassionally I will (or others will) run a command that ends up accidentally creating some goofy named files such as ‘?[01’. This is actually a character so you can’t just type it in to delete it. For this you can lookup the file name by it’s inode and then delete it.

You can do an ls with an -i option to get the inodes, for example:

[$]> ls -ali
total 8
295188 drwxr-xr-x 2 sasowner sas 4096 Dec 30 11:26 .
295064 drwx------ 23 sasowner sas 4096 Dec 30 11:26 ..
295406 -rw-r--r-- 1 sasowner sas 0 Dec 30 11:26 file1
295407 -rw-r--r-- 1 sasowner sas 0 Dec 30 11:26 file2
295408 -rw-r--r-- 1 sasowner sas 0 Dec 30 11:26 file3
295409 -rw-r--r-- 1 sasowner sas 0 Dec 30 11:26 file4

You could then follow this up with a command such as:

[$]> find . -inum 295409 -exec rm {} \;

However, you probably won’t use this command often enough to remember it unless you are regularly familiar with the find command. Instead, I’ve create the following simple function:

function rmi ()
{
find . -inum $1 -exec rm -i {} \;
}

Then simply type the following after getting the inode of the file

[$]> rmi ######
Posted in BASH, Ubuntu - Tagged alias, bash_profile, bashrc, cd, clear, COUNTER, date, directory, monitor, move, navigate, profile, sleep, up, watch

osTicket> Show external ticket ID in browser title.

Aug13
2012
Written by Scott Rowley

 

At my full time job we often reference the ticket ID that we are working on in our daily log, notes or when just when referencing it from person to person. The following will allow for showing the external ticket ID in the browser title.

First we need a new function to reference, find the following in
include/class.ticket.php

/*============== Functions below do not require an instance of the class to be used. To call it use Ticket::function(params); ==================*/

And add the following right after it:

function getExtIdById($id){
$sql ='SELECT ticketID FROM '.TICKET_TABLE.' ticket WHERE ticket_id='.db_input($id);
$res=db_query($sql);
if($res && db_num_rows($res))
list($extid)=db_fetch_row($res);
return $extid;
}

Now open up
include/staff/header.inc.php and add the following just after your <title> tag:

<?if(Ticket::getExtIdById($id)){ echo Ticket::getExtIdById($id)." - "; }?>

Update!
There are a couple of pages you may be on (like “My Preferences”) where the ticket class is not loaded. In order to make sure it’s loaded for use you can added the following just before the title tag:

require_once('../include/class.ticket.php');
Posted in MySQL, osTicket, PHP - Tagged browser, class, external id, internal id, osTicket, php, reference, title

Web Dev> PHP function, check if a MySQL table exists

May01
2012
Written by Scott Rowley

Note: This is NOT original sudobash.net code. This has been copied over from ElectricToolBox.com, full credit goes to him/her (I couldn’t find a name anywhere).

The PHP function below gets passed in a tablename and an optional database name. If the database name is not passed in then it retrieves it using the MySQL function SELECT DATABASE(). It then queries the MySQL information schema to see if the table exists and then returns either true or false.

function table_exists($tablename, $database = false) {
if(!$database) {
$res = mysql_query("SELECT DATABASE()");
$database = mysql_result($res, 0);
}
$res = mysql_query("
SELECT COUNT(*) AS count
FROM information_schema.tables
WHERE table_schema = '$database'
AND table_name = '$tablename'
");
return mysql_result($res, 0) == 1;
}

The PHP MySQL functions are used in the above example. A database connection is assumed and there is no error checking, but you can modify it to utilise whatever database library / abstraction layer you are using in your project and improve how you see fit.

To use the function you’d do something like this:

if(table_exists('my_table_name')) {
// do something
}
else {
// do something else
}

and if you wanted to specify the database name as well (perhaps you are needing to query if the table exists in multiple databases other than the one you are currently connected to), you’d do this:

if(table_exists('my_table_name', 'my_database_name')) {
// do something
}
else {
// do something else
}
Posted in MySQL, PHP, Web Development - Tagged check, database, dev, exists, MySQL, php, table, web, Web Development

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