LinuxQuestions.org
Register a domain and help support LQ
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 10-31-2007, 03:44 AM   #1
xennetwork
Member
 
Registered: Jan 2005
Location: Burney
Distribution: Kubuntu 7.1
Posts: 48

Rep: Reputation: 15
problem with variables and bash


alright im trying to make this bash file convert every video in the current folder into a diffrent format using ffmpeg, but thats not the problem, the problem is that i dont know how to make sed output gointo a variable this is what i have so far
Code:
 #!/bin/bash
for i in $( ls ); do
	if [ $i != "foldertest" ] ; then
		OUTPUTFILENAME="$i | sed -e 's/\.[a-zA-Z]*$//'"
		echo "${OUTPUTFILENAME}.flv"
		exec ffmpeg -i $i $OUTPUTFILENAME
	fi
done
 
Old 10-31-2007, 04:31 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Code:
for video in *.mpg *.avi *.wmv; do
  echo "${video%.*}.flv"
  ffmpeg -i "$video" "${video%.*}.flv"
done
Read through "3.5.3 Shell Parameter Expansion" in the "info bash" manual. Using parameter expansion will be a lot faster, and when you get used to it, easier.

Some commands like test are bash built in commands and will run a lot faster using them compared to running an external command. You can also get quick help on using them by entering "help <builtin>" in the console.

This simple segment demonstrates a test. The spaces just inside the square brackets are manditory. An earlier "sh" shell didn't have [ as a built in so it is command, and is in /bin/[ as well as being a built-in. If you omitted the first space, bash would look for the command "[-f". Not what you wanted.
Code:
for file in *; do
   [ -f "$file" ] || continue
   echo $file
done
I didn't look at the ffmpeg command itself.
 
Old 10-31-2007, 08:09 PM   #3
xennetwork
Member
 
Registered: Jan 2005
Location: Burney
Distribution: Kubuntu 7.1
Posts: 48

Original Poster
Rep: Reputation: 15
i dont think i understand what you want me to do with that code i tryed implementing it and it get more errors then before
 
Old 10-31-2007, 08:30 PM   #4
Dinithion
Member
 
Registered: Oct 2007
Location: Norway
Distribution: Slackware 14.1
Posts: 446

Rep: Reputation: 59
To store the sed output to a variable you could do something like this:
OUTPUTFILENAME=$(echo $i | sed blahblahblah)
 
Old 11-02-2007, 04:14 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
I was showing that you don't need to use sed to change the extenstions of the filenames.
Code:
jschiwal@hpmedia:~> file=sample.mpg
jschiwal@hpmedia:~> out="${file%.mpg}.flv"
jschiwal@hpmedia:~> echo $out
sample.flv
You can use parameter expansion instead which would be faster. The second demonstrated a test for the expansion of the * wild card values which would include not only files but also directories.
Code:
ls
abc.mpg  def.avi  dira  dirb
jschiwal@hpmedia:~/temp> for file in *; do
> [ -f "$file" ] || continue
> echo "$file"
> done
abc.mpg
def.avi
If "$file" isn't a file, the loop continues at the beginning of the loop, otherwise it continues. That matches the logic thay your imaginary directory test performs. You could also test for a file and then conditionally convert the file.
Code:
> for file in *; do
>   if [ -f "$file" ]; then
>     echo ffmpeg -i "$file" "${file%.*}.flv"
>   fi
> done
ffmpeg -i abc.mpg abc.flv
ffmpeg -i def.avi def.flv
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Help With Variables In Bash jamie_h Programming 3 11-01-2006 09:18 AM
Threads synchronisation problem (mutex variables and contitional variables) zahadumy Programming 6 12-07-2005 01:30 PM
variables in bash haddad Linux - General 6 09-22-2004 06:29 PM
Variables in bash tcaptain Programming 1 03-03-2003 03:07 PM
Bash variables pk21 Programming 2 01-09-2003 04:31 PM


All times are GMT -5. The time now is 09:52 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration