LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-11-2013, 01:19 PM   #1
harinathreddy.c
LQ Newbie
 
Registered: Dec 2010
Posts: 26

Rep: Reputation: 1
shell script command


Hi All,

I am writing shell hotplug script,that get run when I insert the USB HD.
But I am facing a problem,we have a proprietary command that is as below.

cfg_write CIFSShares +

If I run that command from busybox(console), sucessfully get run.
If I give same command from hotplug script,that doesnt get run.
I am suspecting that the "+" operater expecting operand.
Even I tried below combinations in the script.

cfg_write is app and CIFSShares, + are arguments.

The script has the redirecting commands as below.
#exec 1>/dev/consol
#exec 2>/dev/consol


1)ret="`cfg_write CIFShares +`"
2)cfg_write "CIFSShares" "+"
3)cfg_write CIFSShares '+'
4)arg1=CIFSShares
arg2="+"
cfg_write $arg1 $arg2
5) echo "#!/bin/sh" > /tmp/cfg_write.sh
echo "cfg_write CIFSShares +" >> /tmp/cfg_write.sh
chmod 777 /tmp/cfg_write.sh

./tmp/cfg_write.sh

With above combination I am not succeed.


But If I uncomment the redirecting commands ( ex:exec 1>/dev/consol instead #exec 1>/dev/consol),no problem at all.

my shell is busy box shell(sh).

Please help out

Thanks,
harinath

Last edited by harinathreddy.c; 01-11-2013 at 01:20 PM.
 
Old 01-11-2013, 02:51 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
I'm more well versed in bash, but here are some suggestions:

- Enable debug within your script, for instance if you perform

Code:
set -vx
in the start of a bash script, it will give you debug line by line as it executes.

- More importantly would be the path variable from within the script, as well as where the executables that the script is running are located.

Where is cfg_write? Is it located in that same directory? Then you may want to consider invoking it from within the script as following

Code:
./cfg_write <followed by arguments>
If it is located somewhere else, perhaps say /bin, then you should invoke it using that full path

Code:
/bin/cfg_write
If you're unsure where, then perform a "which cfg_write" command to get the path location from where cfg_write is located.

Note also that even if you're invoking the executable from the directory where it is located, if your path variable does not have ".", a.k.a. DOT, a.k.a. the local directory as part of the path, then it will not consider binaries or scripts from that local directory, hence why you would use the first rendition utilizing ./<name.

Next also note that the environment within scripts is not always the environment you expect, it depends how you are invoking the script. Since you're running it as part of a hotplug, then you are not manually executing it from a terminal window that you're sitting at, but instead from a potentially different environment.

One trick here is to output your environment to a log file when the script runs

Code:
/usr/bin/env > /home/mylogin/script.log
From there you will get the environment from which the script is being run, you'll see the username, the path, and be able to determine perhaps why cfg_write is or is not visible to that login during the time the script is run.
 
1 members found this post helpful.
Old 01-11-2013, 03:27 PM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
cfg_write $arg1 $arg2
5) echo "#!/bin/sh" > /tmp/cfg_write.sh
echo "cfg_write CIFSShares +" >> /tmp/cfg_write.sh
should be echo "$cfg_write CIFSShares +" >> /tmp/cfg_write.sh

Quote:
cfg_write CIFSShares +

If I run that command from busybox(console), sucessfully get run.
If I give same command from hotplug script,that doesnt get run. ...
/absolute/path/to/cfg_write
in hotplug script?
and/or "$PATH" variable differences.

Start with something like:
Code:
#!/bin/sh
cfg_write="/path/to/cfg_write CIFSShares +"
echo $cfg_write >> /tmp/cfg_write.sh
chmod 777 /tmp/cfg_write.sh
Once you get that working by itself, then you can build "up" on a working script.
Substitute /path/to/ for the actual path:
Code:
whereis cfg_write
All that other "stuff" is convoluted:
Code:
ret="`cfg_write CIFShares +`"
cfg_write "CIFSShares" "+"
cfg_write CIFSShares '+'
arg1=CIFSShares
arg2="+"
cfg_write $arg1 $arg2
I'd copy your original file and start over with less code.

Please use [code][/code] tags.

Last edited by Habitual; 01-11-2013 at 06:56 PM. Reason: s/bash/sh
 
1 members found this post helpful.
Old 01-11-2013, 06:51 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by harinathreddy.c View Post
Hi All,

I am writing shell hotplug script,that get run when I insert the USB HD.
But I am facing a problem,we have a proprietary command that is as below.

...

The script has the redirecting commands as below.
#exec 1>/dev/consol
#exec 2>/dev/consol
...

But If I uncomment the redirecting commands ( ex:exec 1>/dev/consol instead #exec 1>/dev/consol),no problem at all.

my shell is busy box shell(sh).

Please help out

Thanks,
harinath
typographic error there - it is spelled "/dev/console", not /dev/consol.
 
Old 01-15-2013, 01:24 AM   #5
harinathreddy.c
LQ Newbie
 
Registered: Dec 2010
Posts: 26

Original Poster
Rep: Reputation: 1
Hi All,

Thanks to you all.
I tried with all your advises,not helpful so please find the script i am using and let me know if I am doing wrong
over here.

Code:
#!/bin/sh

#set -vx

# send stdout/stderr to the console
#exec 1> /dev/console
#exec 2> /dev/console

case "$INTERFACE" in
	7\/*\/*) ;; # PRINTERs
	8\/*\/*);;  # USB MASS STORAGE DRIVEs
	""|usb?|wifi*|dsl*|wl*|cpcs*|atm*|eth*|ppp*|nas*|br*) exit 0;;
	*) logger -s -p 5 hotplug does not support interface=$INTERFACE; exit 0;;
esac


# wait for lock to be free
while [ -s /var/run/hotplug ]
do
	usleep 500000
done


if [ -s /var/run/hotplug ]
then
	return
else
	echo $PID > /var/run/hotplug
	trap 'rm -f /var/run/hotplug' 0
fi




my_name=mountUsb_script
#To create mount points
mount_dir=/var/ftp/usb_
#Directory in which a file get generated with partition name 
#and that holds the filesystem type.
fstype=/tmp/fs
drive=sd
one=1	

asterisk=*
dlnaconf_file=/var/tmp/minidlna.conf


fatal() {
	logger $my_name: fatal  error: $*
	rm -f  /var/run/hotplug
	exit 255 
}

#Delete unmounted usb config table values
delete_cifsshares_key_value() {
point=$(echo "$1" | sed "s/[^0-9]//g")
if [ "$point" ]; then
	cfg_write CIFSShares.$point.Enabled=0
	cfg_write CIFSShares.$point.Path=""
	cfg_write CIFSShares.$point.Size=""
	cfg_write CIFSShares.$point.Filetype=""
	cfg_write CIFSShares.$point.Key=""
	cfg_write CIFSShares.$point.Partition=""
fi
}
#writing the values to CIFSShares table
write_cifsshares_values() {
	point=$1
	partition=$2
	found1=1
	file_size="`df -h|grep  $partition | awk '{print $2}'`"
	dev="`echo $partition | cut -c-3`"
	cd /var/sys/block/$dev
	signature="`cat device/vendor``cat device/model``cat size`"
	file_type="`cat $fstype/$partition`"
	echo " $point == $partition $file_type $signature $dev" >> /tmp/junk2
	/bin/cfg_write CIFSShares.$point.Enabled=1
	/bin/cfg_write CIFSShares.$point.Key=$signature
	/bin/cfg_write CIFSShares.$point.Size=$file_size
	/bin/cfg_write CIFSShares.$point.Filetype=$file_type
	/bin/cfg_write CIFSShares.$point.Partition=$partition
	/bin/cfg_write CIFSShares.$point.Path="$mount_dir$partition"
	/bin/cfg_write CIFSShares.$point.Comment="Shared USB Drive on Netgear Router"
	/bin/cfg_write CIFSShares.$point.ShareName="Share$point"
}


#logger -s -p 5 INTERFACE=$INTERFACE
#logger -s -p 5 TYPE=$TYPE
#logger -s -p 5 DEVICE=$DEVICE
#logger -s -p 5 SD=$SD
#logger -s -p 5 SERIAL=$SERIAL
#logger -s -p 5 MFG=$MFG
#logger -s -p 5 MDL=$MDL
#logger -s -p 5 MINOR=$MINOR
#logger -s -p 5 PRODUCT=$PRODUCT
#logger -s -p 5 ACTION=$ACTION
#logger -s -p 5 MODALIAS=$MODALIAS
#logger -s -p 5 NOUPDATE=$NOUPDATE


if [ "$ACTION" = "add" ] ; then
		
	found1=0
	send_update=0


	case "$INTERFACE" in
	7/*/*) # PRINTERs
		logger -s -p 5 $0:  USB PRINTER $INTERFACE device $DEVICE minor $MINOR connected

		# mount the usbfs if it doesn't yet exist
		
		if [ ! -n "$(mount|grep usb)" ]
		then
			mount -t usbfs none /proc/bus/usb
		fi

		# check for any usblp devices
		if [ -n "$(cat /proc/bus/usb/devices|grep Driver=usblp)" ]
		then
			found1=1
		fi
		# add usb printer device
		if [ "$MINOR" -gt "-1" -a "$MINOR" -lt "10" ]
		then
			logger -s -p 5 $0: adding usb printer device lp$MINOR
			# test the return code of 'mknod' because new devices get added with a default
			# MINOR of zero.  The kernel has been patched to re-call hotplug once the actual
			# MINOR has been set.  So, if mknod fails because zero is already being used just
			# exit.  It will get called again with the next available MINOR.
			#mknod /var/dev/usblp$MINOR c 180 $MINOR 2>/dev/null;rc=$?

			mkdir -m 755 -p /var/usb
			mknod -m 660 /var/usb/lp$MINOR c 180 $MINOR 2>/dev/null;rc=$?

			if [ $rc = 1 ]
			then
				fatal mknod /var/usb/lp$MINOR failed
				 exit 0;
			fi

			logger -s -p 5 $rc:Invoking StartPrintServer with $MFG and $MDL and $SERIAL and $MINOR 

			startPrintServer "$MFG" "$MDL" "$SERIAL" "$MINOR" 
		else
			logger -s -p 4 $0:  only 10 printers are supported, minor device $MINOR being ignored
		fi
		;;

	8/*/*)  #USB MASS STORAGE DEVICES
		logger -s -p 5 $0: USB STORAGE DEVICE $INTERFACE device $DEVICE minor $MINOR connected
		
		#echo "Attempting to find mount block"

		if [ "$(cfg_read UsbMountConfig.0.Enabled | cut -d "=" -f 2 | cut -d "'" -f 2)" = "0" ]
		then
		exit 0
		fi
		# temporary directory to keep partition filesystem type	
		mkdir -p $fstype

		while [ ! -d /var/sys/block/sd[a-z] ]
		do
			usleep 500000
		done

		for drive in sda sdb sdc sdd; do
			usleep 4000000
		if [ ! -d /var/sys/block/$drive ]; then
			usleep 2000000
		fi

		if [ -d /var/sys/block/$drive ];then 
			# tell the disk_mgmt app to update its info
			# without a mount block the disk_mgmt app can't get any info
			send_update=1
			usleep 500000
			#cd /var/sys/block
			cd /var/sys/block/$drive
		else
			continue
		fi

		while [ ! -d $drive[0-9] ]
		do
			usleep 500000
		done
		echo "`ls | grep $drive`" > /tmp/dev_list.txt	
		#for number in 1 2 3 4; do
		while read partition 
		do

			# make sure the partition is not already mounted
			if [ -f $partition/dev -a -z "$(mount|grep $partition)" ]
            		then
				# this set seems to be diferent than the bash set 
				# it is usable for this, though...
				parts=`cat $partition/dev`
				IFS=: set $parts
				mknod /var/dev/$partition b $*
				# try vfat first....linux mounts vfat as 'msdos' by default, with
				# all the long filename translations
				
				# determine our mount point
				mount_base=/var/dev/$partition
				mount_point=$partition
				mkdir -p $mount_dir$partition

				#echo "the final point $mount_dir$partition"
				usleep 500000
				ntfs-3g /var/dev/$partition $mount_dir$mount_point
				if [ $? -eq 0 ]; then	
				echo "NTFS" > $fstype/$partition
				continue
				fi		
				mount -t vfat /var/dev/$partition $mount_dir$mount_point
				if [ $? -eq 0 ]; then	
				echo "VFAT" > $fstype/$partition 
				continue
				fi
				# Try mounting with unspecified filesystem
				mount /var/dev/$partition $mount_dir$mount_point
				if [ $? -eq 0 ]; then	
				echo "LINUX" > $fstype/$partition
				fi
			fi
			partition=""
		done < /tmp/dev_list.txt
		done
                  
                #Here getting the mounted devices list into the file from mount cmd and reading that filei. 
		#For each insert CIFSShares get updated as bcoz if we insert 2nd disk the first disk 
		#get disconnetd iand get remounted along with 2nd one.
		#echo "#!/bin/sh" > /tmp/cfg_write.sh
		#echo "cfg_write "CIFSShares" "+"" >> /tmp/cfg_write.sh
		#chmod 777 /tmp/cfg_write.sh
			
		
		count=0
		#getting number of rows of the table
		num_rows="`cfg_read CIFSShares | grep "rows" | cut -d"=" -f 2`"
		#collecting mounted devices list to the file
		echo "`mount | grep sd | cut -d" " -f1 | cut -d"/" -f4`" > /tmp/mount.txt
		while read partition
		do
                        #partition=$line
			#if nuber of partition are less than rows
			if [ "$partition" -a  $count -lt $num_rows ]; then
				write_cifsshares_values $count $partition
				count=`expr $count + 1`
			else #if number of partition are more than rows
			if [ "$partition" -a  $count -ge $num_rows ]; then
				/bin/cfg_write CIFSShares +
				write_cifsshares_values $count $partition
                                count=`expr $count + 1`
			fi
			fi
			partition=""
		done < /tmp/mount.txt
               	rm -rf /tmp/mount.txt
		row=$count
		while [ $count -lt $num_rows ]
		do
			#delete_cifsshares_key_value $count
			#deleting the unused rows
			/bin/cfg_write CIFSShares.$row -
			count=`expr $count + 1`
		done					

		if [ "$(cfg_read MiniDLNAServer.0.Enabled | cut -d "=" -f 2 | cut -d "'" -f 2)" = "1" ] ; then 
	   	    kill "`cat /var/run/minidlna.pid`"
		    awk '/db_dir/ {$1 = "db_dir='$mount_dir''$mount_point'"} {print $0}' $dlnaconf_file > $dlnaconf_file'.bak'
		    mv $dlnaconf_file'.bak' $dlnaconf_file
		    usleep 1000000
		    if [ "$(cfg_read MiniDLNAServer.0.AutoScan | cut -d "=" -f 2 | cut -d "'" -f 2)" = "1" ] ; then 
	   	       /bin/minidlna -f $dlnaconf_file -R -P /var/run/minidlna.pid
		    else
	   	       /bin/minidlna -f $dlnaconf_file -P /var/run/minidlna.pid
		    fi
		fi
		;;
	esac
	
	if [ $found1 -gt 0 ]
	then
		# LED on
		while [ ! -n "$(lsmod|grep gpio)" ]
		do
			# wait for gpio to be loaded
			usleep 500000
		done
         #       dmesg > /tmp/UsbInfo.txt
         #       str=`tail -n 20 /tmp/UsbInfo.txt | grep new | grep USB | cut -d':' -f1 | cut -d'-' -f2`
         #       if echo $str | egrep -q '^[0-9]+$'; then
         #       	if [ $str -eq 2 ]
         #       	then 
         #       	 gpioctl -e Usb1Up
         #       	else
         #			 gpioctl -e UsbUp
         #       	fi
         #      else
         #              gpioctl -e UsbUp
         #      fi
         echo "Making the Usb led's on in gpioctl.c file"
	fi

	# tell the disk manager app to update its info
	if [ $send_update -gt 0 -a "$NOUPDATE" != 1 ]
	then
		cfg_msg diskInserted > /dev/null 2>&1
	fi

elif [ "$ACTION" = "remove" ] 
 then
	found1=0


	case "$INTERFACE" in
	7/*/*) # PRINTERs
		logger -s -p 5 $0: USB PRINTER $INTERFACE device $DEVICE minor $MINOR disconnected

		# remove usb printer device
		if [ $MINOR -gt "-1" -a $MINOR -lt "10" ]
		then
			logger -s -p 5 $0: removing usb printer device usblp$MINO

			rm /var/usb/lp$MINOR
			rm -r -f /var/usb
			umount /proc/bus/usb/			
			stopPrintServer $SERIAL

		else
			logger -s -p 4 $0: only 10 printers are supported, minor device $MINOR being ignored
		fi
		;;


	8/*/*) # DRIVEs
		logger -s -p 5 $0: USB STORAGE DEVICE $INTERFACE device $DEVICE minor $MINOR disconnected
        for sdrive in sda sdb sdc sdd; do

		send_update=1
		#umount -lf $mount_dir
		# See if this device is mounted 

		if [ ! -d /var/sys/block/$sdrive ]
		#if [ "$(mount|grep $sdrive)" ]
		then
				echo "unmounting $sdrive"
				logger -s -p 4 $0: unmounting device $sdrive
				# get the mount point(s) of this device
				points=$(mount | grep $sdrive | cut -d " " -f 3)
				for point in $points
				do
					umount $point
					echo " Trying to unmount $point"
					if [ -n "$(mount|grep $point)" ]
					then
						logger -s -p 3 $0: failed to unmount drive from $point going to force unmount
						umount -lf $point
						rm -rf $point
				        	rm -f "$fstype/`echo $point | cut -d"_" -f2`"
						row="`cfg_read -t CIFSShares | grep $point | awk '{print $1}'`"
						if [ "$row" -ge "0" ]; then
                                        	#delete_cifsshares_key_value $row 
						/bin/cfg_write CIFSShares.$row -
						fi
                                	else
                                        	logger -s -p 5 $0: unmounted drive from $point
						rm -rf $point
				        	rm -f "$fstype/`echo $point | cut -d"_" -f2`"
						row="`cfg_read -t CIFSShares | grep $point | awk '{print $1}'`"
						if [ "$row" -ge "0" ]; then
                                        	#delete_cifsshares_key_value $row
						/bin/cfg_write CIFSShares.$row -
						fi
                                	fi
					
				
                                	rm -rf /var/dev/$sdrive$asterisk
	                        done
				#if some how the table is not get cleaned up though the device is diconnected.
				junk=$(cfg_read -t CIFSShares | grep $sdrive | cut -d "/" -f4 | cut -d " " -f1)
				for point in $junk
                               	do
					row="`cfg_read -t CIFSShares | grep $point | awk '{print $1}'`"
					if [ "$row" -ge "0" ]; then
					#delete_cifsshares_key_value $row
					/bin/cfg_write CIFSShares.$row -
					fi
				done
		fi
		done
		logger $my_name: unmounted drive from $mount_dir		
		;;		
	esac

	# if no printer files then assume a possible plug-out case
	# check for any usblp devices
	if [ -n "$(cat /proc/bus/usb/devices|grep Driver=usblp)" ]
	then
		logger -s -p 5 usblp found
		found1=1
	fi 

	# See if we have any disks mounted
	if [ "$(mount|grep $mount_dir)" ]
	then
		found1=1
	fi
	
	# Turn the LED off if we don't have any printers or disks
	if [ $found1 = 0 ]
	then
		# LED off
           #     dmesg > /tmp/UsbInfo.txt
           #     disstr=`tail -n 20 /tmp/UsbInfo.txt | grep disconnect | grep USB | cut -d':' -f1 | cut -d'-' -f2`
           #     if echo $disstr | egrep -q '^[0-9]+$'; then                
           #     	if [ $disstr -eq 2 ]
           #     	then
           #     	 gpioctl -e Usb1Down
           #     	else
	   #		 gpioctl -e UsbDown
           #     	fi
           #     else
           #            gpioctl -e UsbDown
           #     fi
           echo "Making the Usb led's off in gpioctl.c file"
	fi

	# tell the disk manager app to update its info
	if [ $send_update -gt 0 -a "$NOUPDATE" != 1 ]
	then
		cfg_msg diskRemoved > /dev/null 2>&1
	fi
else
	logger -s -p 4 $0: action=$ACTION undefined	
fi
rm -f  /var/run/hotplug




Thanks,
Harinath

Last edited by harinathreddy.c; 01-15-2013 at 03:32 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script, Perl script, command or utility to convert Binary to text Perseus Programming 26 07-12-2012 06:00 AM
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 09:12 PM
Using shell command output as input in shell script - how to do? EnderX Linux - Newbie 2 06-30-2010 12:46 PM
Command in shell script Grassie Coetzee Linux - Software 1 03-13-2005 11:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration