Register
It is currently Wed Jul 23, 2014 5:14 am

Help Using tr and Variables


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Sun Dec 12, 2010 4:22 pm   

Joined: Sun Dec 12, 2010 6:29 am
Posts: 2
I'm just starting out using Bash scripts and I'm doing a bit of test code to try and work stuff out.

I'm trying to take the path of a file (that has been dragged and dropped into the terminal) then I want to remove the quotation marks and store it in a variable for later use in a command.

I've got this bit of code that reads the path, stores it in a variable then spits it out with echo (minus the quotation marks):

Code:
#!/bin/bash
clear
echo "Enter source file name & location"
read SourceFile
SourceFile_cut=$SourceFile
echo $SourceFile_cut | tr -d "'"


What I really want to do is store it in a variable and remove the quotation marks, rather than removing the quotation marks as the variable is echoed, I thought this would work, but it doesn't:

Code:
#!/bin/bash
clear
echo "Enter source file name & location"
read SourceFile
SourceFile_cut=$SourceFile | tr -d "'"
echo $SourceFile_cut


I'm doing this test code to try and improve a Bash script I wrote for utilizing the ffmpeg video encoder, it works fine, it's just annoying having to type in the whole path and file name when dragging and dropping a file is much easier:

Code:
#!/bin/bash
clear
echo "Enter source file name & location"
read SourceFile
echo "Enter maximum file size in bytes (SI prefixes allowed)"
read FileSize
echo "Enter destination file name & location"
read DestinationFile
ffmpeg -i $SourceFile -fs $FileSize $DestinationFile


Top
 Profile  
 PostPosted: Sun Dec 12, 2010 5:34 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 532
Hi,

what you need is Command Substitution
Code:
SourceFile_cut=$(tr -d "'" <<<"$SourceFile")
Bash has Parameter Expansion with which you can do the same thing
Code:
SourceFile_cut="${SourceFile//\'}"


Top
 Profile  
 PostPosted: Sun Dec 12, 2010 6:27 pm   

Joined: Sun Dec 12, 2010 6:29 am
Posts: 2
@Watael - Thank you very much for your help, I used the command substitution and it works great ^_^

If anyone's interested, here's my ffmpeg bash script with the alterations included:

Code:
#!/bin/bash
clear
echo "Enter source file name & location"
read SourceFile
SourceFile_cut=$(tr -d "'"<<<"$SourceFile")
echo "Enter maximum file size in bytes (SI prefixes allowed)"
read FileSize
echo "Enter destination file name & location"
read DestinationFile
DestinationFile_cut=$(tr -d "'"<<<"$DestinationFile")
clear
ffmpeg -i $SourceFile_cut -fs $FileSize $DestinationFile_cut


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP