LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-10-2010, 01:30 PM   #1
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
need help with sed for a shell script i'm working on


to give a brief background on what I'm trying to do, simply put, I have configured dosbox to use my external midi keyboard (a usb to midi controller attached to a Casio keyboard)

on occasion the midi port number assigned to the midi cable changes (I move the computer often so the cable gets unplugged often), which requires me to manually change the number in ~/.dosbox/dosbox-0.xx.conf file, not a big deal really but it would be easier if i could do the change with a shell script

i have started on said said script and using grep, cut and if then statements have managed to compare the 2 values, now i just need a sed statement to actually perform the replacement and write the information back to the file

here is my script so far:
Code:
#!/bin/bash
# get the port number of the usb midi device
export alsa=`pmidi -l | grep USB | cut -d' '   -f2`
# check if a port number is present
if [ -z $alsa ]
	then
		#if nothing found, report this to the user and stop
		echo 'no usb midi device found'

	else
		#aquire version of dosbox (used for the .conf file name)
		export version=`dosbox --version | grep version | cut -d' ' -f 3 | cut -d, -f 1`
		#report dosbox version found
		echo 'dosbox version: '$version;
		#report the current port number for the USB device
		echo 'found using pmidi -l:' $alsa
		#search the file for the port number dosbox is currently trying to use
		export file=`cat ~/.dosbox/dosbox-"$version".conf | grep midiconfig= | cut -d= -f2`
		#report the port number found in the dosbox.conf
		echo 'found in dosbox conf file:' $file
		#compare th e values;
		if [ "$file" = "$alsa" ]
			then
			    #if the dosbox.conf is current, report this to the user and stop
			    echo 'match found nothing to do'
			else
			    #replace the contents of the file and report the change has been made
			    echo 'need sed statement to replace id found in dosbox.conf with output of pmidi -l'
			    echo 'file updated'
		fi
fi

Last edited by frieza; 07-10-2010 at 01:36 PM.
 
Old 07-10-2010, 02:45 PM   #2
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Original Poster
Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
nvm i figured it out
the finished script
Code:
#!/bin/bash
# get the port number of the usb midi device
export alsa=`pmidi -l | grep USB | cut -d' '   -f2`
# check if a port number is present
if [ -z $alsa ]
	then
		#if nothing found, report this to the user and stop
		echo 'no usb midi device found'

	else
		#aquire version of dosbox (used for the .conf file name)
		export version=`dosbox --version | grep version | cut -d' ' -f 3 | cut -d, -f 1`
		#report dosbox version found
		echo 'dosbox version: '$version;
		#report the current port number for the USB device
		echo 'found using pmidi -l:' $alsa
		#search the file for the port number dosbox is currently trying to use
		export file=`cat ~/.dosbox/dosbox-"$version".conf | grep midiconfig= | cut -d= -f2`
		#report the port number found in the dosbox.conf
		echo 'found in dosbox conf file:' $file
		#compare th e values;
		if [ "$file" = "$alsa" ]
			then
			    #if the dosbox.conf is current, report this to the user and stop
			    echo 'match found nothing to do'
			else
			    #replace the contents of the file and report the change has been made
			    sed -i 's/'$file'/'$alsa'/g' ~/.dosbox/dosbox-"$version".conf
			    echo 'file updated'
		fi
fi
 
Old 07-10-2010, 06:58 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,139

Rep: Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122
Single and double quotes generate different behaviour. I prefer doubles in that situation
Code:
sed -i "s/$file/$alsa/g"
or even better ...
"s/${file}/${alsa}/g"
 
  


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
[SOLVED] shell script error (sed) MartenH Linux - Newbie 1 02-21-2010 02:17 PM
shell script, sed nushki Linux - Newbie 1 03-31-2009 01:36 PM
Shell Script , using Sed mogra Linux - Newbie 8 05-07-2008 01:18 PM
Using sed in a shell script RobHill Linux - General 4 05-29-2007 03:31 PM
shell script problem on sed. chooi Programming 3 02-10-2006 11:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:29 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