Linux – run vlcplayer as root

When you try to run vlcplayer as root you might encounter below message.

VLC is not supposed to be run as root. Sorry.
If you need to use real-time priorities and/or privileged TCP ports
you can use vlc-wrapper (make sure it is Set-UID root and
cannot be run by non-trusted users first).


By using the trick below you should be able to run vlcplayer as root.(This was tested in Ubuntu 12.04.4 LTS)

sed -i 's/geteuid/getppid/g' /usr/bin/vlc

Now you should be able to run vlc with root.

Linux – Configure multiple network with iproute2

Iprout2 can be used to access two different networks in parallel from a Linux machine.If you have a Linux machine with interfaces eth0 and eth1, and you want to access internet through eth0 and use eth1 to access restricted resources.you can configure iproute2 to achieve  this.

Consider eth0 and eth1 has below ip addresses and subnets.

eth0(internet)

ip:172.16.0.188
netmask:255.255.0.0
gateway: 172.16.0.4

eth1(private)

ip: 10.105.12.8
netmask: 255.255.255.192
gateway: 10.105.12.62

Now add the script below in your bashrc file to make it permanent after reboot.

-------------------START-------------------
#Verify if a rule is already present and create with name 100 if not present

ip rule show | grep -i 100  > /dev/null
if test "$?" -ne "0";then

ip route add 10.105.12.0/26 dev eth1 src 10.105.12.8 table 100
ip route add default via 10.105.12.62 dev eth1 table 100

ip rule add from 10.105.12.8/32 table 100
ip rule add to 10.105.12.8/32 table 100
fi

#Configure/add  individual routes if needed
route -n | grep -w "10.105.12.62" > /dev/null

if test "$?" -ne "0"; then
route add -net 10.103.20.0/24 gw 10.105.12.62
route add -net 10.103.20.0/24 gw 10.105.12.62
fi
-------------------END-------------------

Bash – Convert column to row

If you have a file with single column and you want to convert it in a single row, you can use the below shell command .

Suppose you have text file(hw.txt) with following column.

cdrom
cpu
disk
ide
memory
netcard
partition
printer
scanner
scsi
gfxcard
sound
usb
wlan
bluetooth
monitor

Entries above can be aligned horizontally by executing the command below.

for i in `< hw.txt`; do echo -n ${i}" ";done; echo ""

It will output

cdrom cpu disk ide memory netcard partition printer scanner scsi gfxcard sound usb wlan bluetooth monitor

NOTE: "echo -n" does the trick.

Insert into mysql from text file

If you have text file with multiple columns separated by space(or something else). you want to take entries from each row and  insert into MySQL database from a text file.

Following steps should be useful.

input.txt is a text file with below entries

105  bill@<domain.com>
304  lance@<domain.com>
605  foo@<domain.com>
609  bar@<domain.com>

inputfile="input.txt"
cat $inputfile | while read id map; do
echo "INSERT INTO <tablename>(users_id,email,email_map) VALUES ('$id','$map', '$map');"
done | mysql -u<username> -p<password> <database>;

Script to check for new file in a folder every 2 minutes

The script below will checks for new file(s) in provided directory and perform some task.

---------------------START---------------------

#!/bin/bash

last_list="/tmp/last_list"
files_dir="/var/media/films"
new_list="/tmp/new_list"
script="/var/media/fetch_asset_info.sh"

 

#loop infinitely
while : ; do
`ls ${files_dir}/*.mpg2.* | grep -v "temp$" | awk -F'/' '{print $(NF)}' > ${new_list}`

cmp -s "$new_list" "$last_list"  > /dev/null
if test "$?" -ne "0";then
for fileNew in `diff ${last_list} ${new_list} | grep ">" | cut -f2 -d" "`;
do
echo "New file is $fileNew"

#Performing specific task when new files copied in the directory
$script $fileNew
done
echo "Operation Completed"
echo "Updating Old file"
`cp ${new_list} ${last_list}`
fi
echo "Waiting for 2 minutes"
sleep 120
done

---------------------END---------------------