Linux Utilities - Scripts in Bash and TCL


Last Update: 12th July 2015.

Contents

Bluetooth file transfer from Mobile Phone
Works only for systems with the blueman suite.

Compressing .AVI files - avicompress

Benchmark CPU Test 1000 Additions - bmark

System information - grabsysinfo.sh

Icondesk Scripts
Manipulate generation and update of icondesk desktop icons.

Lxde Scripts
Manipulate LXDE icons - not all LXDE versions have a built-in facility for that. This works on ALL of them.

Bluetooth file transfer from Mobile Phone
#!/bin/bash
clear
echo "bluexfer -  File transfer mobile phone to PC via bluetooth"
echo "Plug in the USB bluetooth dongle then hit enter"
read dummy
rfcomm release all
echo "Any error messages? If not hit Enter else ^C"
read dummy
hcitool dev
echo "Did it get the dongle noticed? If yes, hit Enter and proceed, else ^C, remove dongele and re-boot."
read dummy
echo "Start of blueman-services coming up!"
echo "before that happens please:"
echo "Enable on the phone Bluetooth and Bluetooth-visibility for your files if you haven't done so yet."
echo "When the window has popped up:"
echo "Enable on it Transfers, File Receiving (Object Push) and File Sharing (FTP). Accept it. THEN CLOSE THE WINDOW."
echo "Hit Enter when the window is closed."
############################################
blueman-services &
############################################
read dummy
clear
echo "Next window: Start of blueman-services coming up."
echo "Enable on this window Visibility to always Visible and note the given Friendly Name. THEN CLOSE THE WINDOW."
echo "Then select on your phone the files you want to transfer and send them via Bluetooth to the Friendly Name"
echo "Accept them on  the PC when asked by a pop-up window."
echo "Hit Enter when all is done."
############################################
blueman-adapters &
############################################
read dummy 

Compressing .AVI files - avicompress
#!/bin/bash
# param1 = directory of the files, param2 = length of filename before . -1
if [ -z $2 ]
  then 
  echo "No length specified - parameter 2"
  exit 4
fi
cd $1
for xfil in *.AVI
do
 mencoder -ovc lavc -oac copy $xfil -o ${xfil:0:$2}.avi
done
exit 

Benchmark CPU Test 1000 Additions - bmark
#!/usr/bin/tclsh
puts "Benchmark to perform 1000 additions"
set timval [time {
  for {set i 0} {$i<1000} {incr i} {
      # empty body
      }
} 10 ]
puts $timval
exit

System information - grabsysinfo.sh
#!/bin/bash
# grabsysinfo.sh - A simple menu driven shell script to to get information about your 
# Linux server / desktop.
# Author: Vivek Gite
# Date: 12/Sep/2007
 
# Define variables
LSB=/usr/bin/lsb_release
 
# Purpose: Display pause prompt
# $1-> Message (optional)
function pause(){
	local message="$@"
	[ -z $message ] && message="Press [Enter] key to continue..."
	read -p "$message" readEnterKey
}
 
# Purpose  - Display a menu on screen
function show_menu(){
    date
    echo "---------------------------"
    echo "   Main Menu"
    echo "---------------------------"
	echo "1. Operating system info"
	echo "2. Hostname and dns info"
	echo "3. Network info"
	echo "4. Who is online"
	echo "5. Last logged in users"
	echo "6. Free and used memory info"
	echo "7. exit"
}
 
# Purpose - Display header message
# $1 - message
function write_header(){
	local h="$@"
	echo "---------------------------------------------------------------"
	echo "     ${h}"
	echo "---------------------------------------------------------------"
}
 
# Purpose - Get info about your operating system
function os_info(){
	write_header " System information "
	echo "Operating system : $(uname)"
	[ -x $LSB ] && $LSB -a || echo "$LSB command is not insalled (set \$LSB variable)"
	#pause "Press [Enter] key to continue..."
	pause
}
 
# Purpose - Get info about host such as dns, IP, and hostname
function host_info(){
	local dnsips=$(sed -e '/^$/d' /etc/resolv.conf | awk '{if (tolower($1)=="nameserver") print $2}')
	write_header " Hostname and DNS information "
	echo "Hostname : $(hostname -s)"
	echo "DNS domain : $(hostname -d)"
	echo "Fully qualified domain name : $(hostname -f)"
	echo "Network address (IP) :  $(hostname -i)"
	echo "DNS name servers (DNS IP) : ${dnsips}"
	pause
}
 
# Purpose - Network inferface and routing info
function net_info(){
	devices=$(netstat -i | cut -d" " -f1 | egrep -v "^Kernel|Iface|lo")
	write_header " Network information "
	echo "Total network interfaces found : $(wc -w <<<${devices})"
 
	echo "*** IP Addresses Information ***"
	ip -4 address show
 
	echo "***********************"
	echo "*** Network routing ***"
	echo "***********************"
	netstat -nr
 
	echo "**************************************"
	echo "*** Interface traffic information ***"
	echo "**************************************"
	netstat -i
 
	pause 
}
 
# Purpose - Display a list of users currently logged on 
#           display a list of receltly loggged in users   
function user_info(){
	local cmd="$1"
	case "$cmd" in 
		who) write_header " Who is online "; who -H; pause ;;
		last) write_header " List of last logged in users "; last ; pause ;;
	esac 
}
 
# Purpose - Display used and free memory info
function mem_info(){
	write_header " Free and used memory "
	free -m
 
    echo "*********************************"
	echo "*** Virtual memory statistics ***"
    echo "*********************************"
	vmstat
    echo "***********************************"
	echo "*** Top 5 memory eating process ***"
    echo "***********************************"	
	ps auxf | sort -nr -k 4 | head -5	
	pause
}
# Purpose - Get input via the keyboard and make a decision using case..esac 
function read_input(){
	local c
	read -p "Enter your choice [ 1 - 7 ] " c
	case $c in
		1)	os_info ;;
		2)	host_info ;;
		3)	net_info ;;
		4)	user_info "who" ;;
		5)	user_info "last" ;;
		6)	mem_info ;;
		7)	echo "Bye!"; exit 0 ;;
		*)	
			echo "Please select between 1 to 7 choice only."
			pause
	esac
}
 
# ignore CTRL+C, CTRL+Z and quit singles using the trap
trap '' SIGINT SIGQUIT SIGTSTP
 
# main logic
while true
do
	clear
 	show_menu	# display memu
 	read_input  # wait for user input
done

Icondesk Scripts

They consist of Ideskr, Idesknewic, Idesknew, Ideskrm and Ideskeddel.
These are all bash scripts. They use auxilliary commands eog, xterm and Xdialog (Xdialog-2.3.1-2.mga1.rpm).

Ideskr - Reload icondesk.
#!/bin/bash
killall idesk
idesk &
exit 0

Usage example (icondesk icon):
table Icon
  Caption: Reload
  ToolTip.Caption: Restart idesk
  Icon: /home/waldis/icons/forms_32x32.xpm
  Width: 32
  Heigth: 32
  X: 21
  Y: 75
  Command[0]: ideskr
  Command[1]: ideskrm ~/.idesktop/ideskr.lnk
end


Idesknewic - Create a new icon and subsequently reload icondesk. 
#!/bin/bash
xterm -geometry 640x480+0+0 -e idesknew
killall idesk
idesk &
exit 0

Usage example (icondesk icon):
table Icon
  Caption: New Icon
  ToolTip.Caption: generate one
  Icon: /home/waldis/icons/kedit_32x32.xpm
  Width: 32
  Heigth: 32
  X: 25
  Y: 19
  Command[0]: idesknewic
  Command[1]: ideskrm ~/.idesktop/New.lnk
end

It uses the script idesknew, which MUST be run in an xterm terminal window :
#!/bin/sh
XDIALOG_HIGH_DIALOG_COMPAT=1
cd ~/.idesktop
filex=1
suffx=".lnk"
until [ $filex -eq 0 ]
do
  echo "Creating new IDESK item"
  echo "Name in directory, suffix .lnk will be appended :"
  read nenam
  nename="$nenam$suffx"
  if [ -f $nename ] 
  then
    echo "This file DOES ALREADY EXIST. Try again."
  else  
    filex=0
  fi
done
touch "$nename"
echo "table Icon">$nename 
echo "Enter Caption (displayed on desktop):"
read captname
echo "  Caption: $captname">>$nename
echo "Enter Caption-Tip (further explanation):"
read catip
echo "  ToolTip.Caption: $catip">>$nename
loopend0=0
until [ $loopend0 -eq 1 ]
do
 loopend1=0
   until [ $loopend1 -eq 1 ]
   do
      echo "Select icon-file. Hit ENTER to start"
      read dummy
      clear
      icfnam=$(Xdialog --title "Select an icon" --stdout --ok-label "done" --fselect ~/ 24 80)
          if [ -f $icfnam ] 
          then
	     echo "look at your selection, then close the displayed window"
	     display $icfnam
	     echo "Do you like that selection? 1/0"
	     read likeit
	     if [ $likeit -eq 1 ]
	     then
                filex=1
                loopend1=1
             fi		
          else
             echo "Something went wrong with your selection. Try again."
          fi
   done
 if [ -z $icfnam ]
 then
   echo "Incorrect selection or cancel hit - try again"
 else 
   loopend0=1
 fi
done
echo "Icon = " $icfnam
echo "  Icon: $icfnam">>$nename
echo "Width of icon in pixels (32):"
read wipic
if [ $wipic -z ] 
then
  wipic="32"
fi
echo "  Width: $wipic">>$nename
echo "Heigth of icon in pixels (32):"
read hipic
if [ $hipic -z ] 
then
  hipic="32"
fi
echo "  Heigth: $hipic">>$nename
echo "Initial X-location of icon (500):"
read xloc
if [ $xloc -z ] 
then
  xloc="500"
fi
echo "  X: $xloc">>$nename
echo "Initial Y-location of icon (500):"
read yloc
if [ $yloc -z ] 
then
  yloc="500"
fi
echo "  Y: $yloc">>$nename
echo "Command to execute as  primary:"
read comma
echo "  Command[0]: $comma">>$nename
echo "  Command[1]: ideskrm ~/.idesktop/$nename">>$nename
echo "end">>$nename
echo "Your chance to make any changes"
inmed="$nename###"
Xdialog --title "Edit file if you want to change anything" --stdout --ok-label "done" --editbox $nename 17 80 1>$inmed
if [ $? -eq 0 ]
   then
     mv -f $inmed $nename
   else
     rm -f $inmed
   fi
cd
exit 0

Ideskrm - Edit or remove an icon plus restart icondesk.
Must not be run in a terminal window: 
#!/bin/bash
xterm -e ideskeddel $1
killall idesk
idesk &
exit 0

It uses script ideskeddel, which MUST be run in a terminal window:
#!/bin/bash
ansok=0
until [ $ansok -eq 1 ]
do
  echo "Edit(1) or delete(2) item?"
  read answ
  if [ $answ -eq 1 ]
  then
    clear
    inmed="$1###"
    Xdialog --title "Edit file" --stdout --ok-label "done" --editbox $1 17 80 1>$inmed
    if [ $? -eq 0 ]
    then
       mv -f $inmed $1
    else
       rm -f $inmed
    fi
    ansok=1
  elif [ $answ -eq 2 ]
    then
      rm -f $1
      ansok=1
    else 
    ansok=0
    echo "Reply 1 or 2 ONLY"
  fi
done      
exit 0

Lxde Scripts

So far I have lxdnew to create a new link to an application on $HOME/Desktop - wrapper for lxshortcut:
#!/bin/bash
cd ~/Desktop
echo "Creating new LX-DESKTOP item"
echo "Name in directory, suffix .desktop will be appended :"
read nenam
lxshortcut -i 1 -o $nenam
cd
exit 0

Return to my Linux-page
Return to my home-page