Ubuntu :: Bash Script - For Loop - Replacing Strings?
Aug 16, 2011
I'm not overly great with bash scripting. I do more batch file stuff for Windows than anything else but I'm trying to branch out a bit. (Frankly, I'd love to do this in Ruby but I'm not there yet...) Maybe I'm not using the correct terminology and that's why my searches are coming up useless...
Anyway, I have a script that does some work with HandBrake. Although it deals with HandBrake, I think the issue is not with HandBrake itself. The issue: I had a simpler script which worked fine but it would give me an MP4 with a file name of: Some Movie Name.iso.mp4 and it would still play but I wanted to get rid of the .iso in the file name. I also wanted to drop it into another directory during the encoding. That's why you see a $source & a $destination variable.
handbrake.sh
Code:
#!/bin/bash
source=/media/data/movies
destination=/media/data/movies/mp4s
[Code]...
View 7 Replies
ADVERTISEMENT
Jul 29, 2010
I want to search and replace strings in a file with strings in other files/i need to do it with big strings(string1 is big) and i want to use a txt file for this.But this code not working :
View 14 Replies
View Related
Aug 22, 2010
i'm trying all the time to use this (find,sed gnu,..etc) scrip to find and remove this string in all files *.php in wwwbackup/ directory. the script work without any error, but doesn't remove any thing!?
find /home/usr/wwwbackups/ -type f -name *.php -print0| xargs -0 sed 's#echo "<iframe src="http://internetcountercheck.com/?click=2255046" width=1 height=1 style="visibility:hidden;position:absolute"></iframe>";##g' -i
how can i become this script working?
View 8 Replies
View Related
Jan 15, 2010
fakeFile's contents :
Code:
blah $(date +%F) blah
fakeScript's contents :
Code:
while read line; do
echo $line
done < "fakeFile"
so here's the problem : is there any way to evaluate the fakeFile's contents (in the fakeScript), so that the term "$(date +%F)" (or any other bash script) in a string , translates to it's corresponding value ? (in this example , we want to have "blah current date" instead of " blah $(bash script) blah " )
View 9 Replies
View Related
Mar 3, 2010
How do you add strings to the front and end of variables in bash scripting? for example, if I have
Code:
in as the contents of my variable, I want to add "l" to the front and "k" to the back of the contents.
View 1 Replies
View Related
Oct 5, 2010
I've been surfing and searching the net quit a while now to make my own script, but I haven't been really successful ever since I want to make a script which can remove strings from my mp3 collection (file names).
For example:
Code:
101-bob_sinclar_feat_sean_paul-tik_tok_(radio_edit).mp3 --> bob_sinclar_feat_sean_paul-tik_tok_(radio_edit).mp3
10-Young Jeezy-Lose My Mind (78 Bpm) (Repack).mp3 --> young_jeezy-lose_my_mind.mp3
Now the problem is how can I remove the strings like:
101 & 10 (dynamic)
(%%% Bpm) (dynamic)
(Repack) (static)
View 11 Replies
View Related
Nov 16, 2010
I have multiple strings (eg. say two, firstLIST=(0 1 2) and secondLIST=(2 3)) and want to create a single string composed of their unique sorted elements. For the sample strings above, I'd like to build masterLIST=(0 1 2 3).I suppose I could write the elements of firstLIST and secondLIST to files
Code:
echo ${firstLIST[@]} > firstFILE
echo ${secondLIST[@]} > secondFILE
then use
Code:
sort firstFILE secondFILE | uniq > masterFILE
as this gives me a file populated with the elements I'm after, but I'm not sure how to read the elements back into masterLIST... and it doesn't seem "right" to create files to accomplish this. Is there a way to do this by manipulating the strings ${firstLIST[@]} and ${secondLIST[@]} directly? The closest I've come (not close at all) is
Code:
masterLIST=${firstLIST[@]}" "${secondLIST[@]}
but masterLIST built this way has only one element
Code:
$echo ${masterLIST[@]}
0 1 2 2 3
$echo ${#masterLIST[@]}
1
and I don't have access to the individual digits to then try to figure out how to remove duplicates.
View 4 Replies
View Related
Mar 13, 2011
I'm trying to write a bash script to find all lines containing two different strings in many files. I don't have access to egrep so I want to use sed for this purpose.
The files will look like this:
FileX
------
Info:18
Data:76
Contact:me@home.com
Start:1500
I want to generate a new file from these files with only the rows containing Data and Start. Something like this:
for y in `ls /file*.db`;
do sed '/Data|Start/p' $y > newfile
done
View 7 Replies
View Related
Apr 30, 2010
...and returning the index of the found element in its array.
I have:
for ((i=0; i < ${#array1[@]}; i++)); do
# Read each line of the file test
if [[ $(eval "sed -n '$(($i+1))'p test") == *${array2[0]}* ]]
stuff
I want to find the index of the found substring in array2 and only if it isn't found, move on to the next element of array2. I don't know the size of array2 so that [0] has just got to go.
View 14 Replies
View Related
Mar 11, 2011
I'm executing the below bash script, but when the m variable doesn't get any value it becomes empty. I don't want that.
Code:
for i in "$@"
do
m=$(grep TOTAL_MAPS $i | awk '{ print $2}');
totalmaps+="<string>"$m"</string>"
[Code]....
View 6 Replies
View Related
May 25, 2010
I have lines in some files that look exactly as below, and the line numbers they occur in are always the same. (Lines 136-139)
W 0.00000000 0.00000000 2.00000000
W 0.50000000 0.50000000 2.50000000
W 0.00000000 0.00000000 3.00000000
[code]...
View 1 Replies
View Related
Jan 9, 2010
This gives a selection menu where you choose an option, and it should continue. I have several of these as part of a much larger script, but something is wrong with this while loop.
Code:
VARIABLEINPUT="1"
if [ $VARIABLEINPUT == "1" ] ; then
echo -e "${YELLOW}How often would you like the automatic update and maintenance to occur after the installation?:
${RED}1)${WHITE} Hourly
${RED}2)${WHITE} Daily
${RED}3)${WHITE} Weekly
${RED}4)${WHITE} Monthly
"
[Code].....
A very simple loop that uses a variable from earlier in the script to adjust variables for later in the script. For some reason, this while loop will not end - I've noted where with echos.
View 4 Replies
View Related
Jul 18, 2010
I have a while loop in a file that looks like:
Code:
while IFS=":" read name script
do
su
exit
[code]....
Where I redirect the file into the loop, for some reason, I can't do an su when I redirect a file like that. I get the error, "su: must be run from a terminal." Why is this? How can I fix it?
View 7 Replies
View Related
Mar 19, 2010
I am trying to make a bash menu that loops with options but it does not work as I want:
Code:
I want to make it read an option and do the action then return to menu.
View 2 Replies
View Related
May 15, 2010
What is the difference between For and While when creating loops in bash scripting?
View 1 Replies
View Related
Aug 2, 2010
I have a file in the form below, and wish to replace each start line with an increasing number. So instead of:
Code:
start
content content
start
content content
start
[Code]....
After several searches and a bit of messing around, it's clear I'm missing something, so was wondering if anyone could offer any insight?
View 17 Replies
View Related
Apr 21, 2010
I have bash 4.0. I used the following for loop example, but all it outputs is "{1..10} instead of the actual numbers one through ten. why?
Code:
for a in {1..10}
do
echo -n "$a "
done
View 5 Replies
View Related
Mar 17, 2010
I want to figure out what is going on in this small script.Its really strange.I think its the infinite loop again.All I want to do is to collect some data from the zenity dialog box in an array and then echo it.Here is the code
Code:
#!/bin/bash
#export PS4='+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}: ' debugging info
[code]...
View 5 Replies
View Related
May 13, 2010
Totally new to programming (as of this morning) so please bear with me I have a bash script that stops working after the first while loop.
#part 1
While
doThisThatThe Otherdone
#part 2
While
doMOreMoreMoredone
Every arguement above the first loop completes (including the first loop itself) but anything below (incuding the second while loop) doesn't finish. I know that it isn't a problem with an unending loop... it seems to be something about the While loop that I'm not understanding... but I can't figure it out. I just need arguments below the first while loop to take place.
View 7 Replies
View Related
Jul 7, 2010
I have to format 4 years worth of awstats data "static" for a client and then move it to their new server.I don't want to run the commend to do this 48 times. If possible I would like to use a bash script that uses the folders in a directory so the script knows which year-month to do this for me and which folder to place the output in.
View 5 Replies
View Related
Dec 6, 2010
I ran across some odd behaviour using ssh in a bash script. I am hoping someone can explain it to me. I have a file called /home/user/hosts.
Code:
$ cat /home/user/hosts
host1
host2
host3
What I want (for this example anyway) is to run a script that checks this file and for each entry in this file it should log into that host and run a command. I have a script called testssh.sh.
Code:
$ cat /home/user/testssh.sh
#!/bin/bash -
somefile='/home/user/hosts'
[code].....
The oddity that wasted an hour of my day and the question I have for this group is, what is it about ssh that breaks the while loop? It obviously runs through once and checks the first host, just never checks any of the others. I checked exit status's and all kinds of verbose data and I could not figure it out.
View 3 Replies
View Related
Nov 18, 2010
I'm writing a mass snmp toner check which polls any toners available to be snmp polled, however when using a loop statement I get the results on different lines; which sounds good, however the tool I use to check with (nagios) ignores the new lines.
Is there any way I can get the output on one line? Also, I need to raise a fault if any of the toners are below a specific level (with nagios you raise faults with the exit code) - any way I can do this without exiting the loop. Code below with bits and bobs commented out.
Code:
check_ink_levels ()
{
for ID in $INKS
do
[Code].....
View 10 Replies
View Related
Jun 7, 2010
The script that Iam trying to write is running a for loop and reading line by line from a text file. inside this for loop i would like to execute update SQL statement .
a pesudo code is
Quote:
`$ISQL -U $username -P $Password -D $Dbname -I $INTERFACE <<QRY
for id in $idlist #idlist iam reading from a file
[code]...
View 8 Replies
View Related
Jan 1, 2011
I have a file like below:
PU12829,24869;PD15733,24869;PD15733,19785;PD12829,19785;PD12829,24869;
PU4599,20915;PD9924,20915;PD9924,18898;PD4599,18898;PD4599,20915;
PU12829,24869;PD15733,24869;PD15733,19785;PD12829,19785;PD12829,24869;
PU4599,20915;PD9924,20915;PD9924,18898;PD4599,18898;PD4599,20915;
PU1723,3423; #this line is ignored to short
[Code]...
What I'm trying to do is while true, cut each line from file that begins with PU and thats longer than 12 characters and write to a increasing numbered file for each line. Stating with object1 etc.
View 14 Replies
View Related
Jul 3, 2009
I know of 4 different ways to use a for loop:
1. for I in {1..10}; do echo $I; done|
2. for I in 1 2 3 4 5 6 7 8 9 10; do echo $I; done|
3. for I in $(seq 1 10); do echo $I; done|
4. for ((I=1; I <= 10 ; I++)); do echo $I; done
I have a script which uses the 1st form of for loop. I'm trying to modify it to use a variable instead of a static hard-coded value in the section that controls the looping.of the for loop.
I've tried all different ways of quoting and escaping the variable, and the problem is that the quoting chars and escape char are being translated and passed into the loop along with the value stored in the variable.
For example, to change the start value of 1 to whatever value I want passed in through a variable:
Change:
I have tried: {{$a}..10} and {`$a`..10}, to have the variable evaluated first.
I have tried using the eval() function.
I have tried single and double quotes and the backslash escape character.
Nothing I've tried works. It's probably a syntax error.
View 14 Replies
View Related
Aug 12, 2011
nested loop in ''Bash-Scripting in Linux'' ?What's wrong with my code:
for x in `seq 0.75 0.01 0.95`
do
for y in `seq 0.20 0.01 0.40`
[code]....
View 1 Replies
View Related
May 30, 2010
Am new in bash scripting, presently I have 2 files and i need to create a file reading from these 2 files. I was trying to work with 2 for loops, but I can see like once its executing 1st outer loop then all inner loop, then next outer loop+ all inner loop. How can i get result like 1 outer loop, 1 inner loop then 2 outer loop,2 inner loop etc. Below is my prog
#!/bin/bash
rm d
for i in `cat a`
do
echo "dn:$i >> d
for j in `cat b`
[Code]...
View 6 Replies
View Related
Mar 1, 2011
As you can see on the output of the script, the two 'testing echoes' I do at the end don't print anything.That's the point, I do NEED this array further in my script.I'd understand my "param" var is local to the for, but is the other one too ? I tried to use "declare -a file" before the for, but i get same exact result !
View 2 Replies
View Related
Feb 2, 2010
Never mind, I figured it out myself. Firstly, the old version of BASH I'm using doesn't support
Code:
for i in {1..27}
So I had to use
Code:
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
Secondly, it was simply
Code:
#!/bin/bash
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
do
[code]....
View 2 Replies
View Related
May 26, 2011
So I need to write a bash script that can read lines and column 3 from a file. It needs to start on line 16 and read every 20th line starting from there. But the value that it reads needs to be checked, should it be too great I need it to shut the program down.I'm pretty new to bash, but my ultimate goal is being able to safely run a program on a GPU for an extended period of time with out worrying about it overheating. I have a command that outputs information from the GPU every second, and I can save this to a file. So all I really need is something to read and check that file, I played around a bit with the awk command and can't get it to work within my for loop with dynamic variable.
View 7 Replies
View Related