Solved

Bash shell linux how to copie line by line on text files in a variable

Posted on 2014-09-30
20
193 Views
Last Modified: 2014-10-09
hello

Bash shell linux how to copie line by line on text files in a variable

Exemple
text files
www.website.me/348744jnm
www.website.me/43545ljkjjk
www.website.me/554erer5cf

Open in new window



the script should read line 1 and copy in  a variable  $url  
and returns to the beginning of the script to use the variable and then passes the second line
and when it has rained link the script ends

tx
0
Comment
Question by:Gaaara
  • 11
  • 9
20 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40351736
while read url
   do
       echo $url
       ## do whatever with $url
   done < textfile
0
 

Author Comment

by:Gaaara
ID: 40351755
hello tx is working !!!!

just one question
I already have a script with a loop and want to integrate it into my code double loop

my code is here
https://code.stypi.com/msii22lh
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40351803
Where in that construct can we find the file containing the urls?
Where should the nested loop be placed?
0
 

Author Comment

by:Gaaara
ID: 40351872
in my script I want to offer two possible entering the links manually or with the text file in the same code

exemple
www.website.me/348744jnm
www.website.me/43545ljkjjk
www.website.me/554erer5cf

Open in new window

I have 3 links in my text file and the script will download then mux switch to another mux download links etc
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40351878
By now your script offers just the option of entering text.
Add the otion of entering the name of a file then process that file the way I showed you.
Maybe you sould test for the file's existence first:

if [[ -e <variable_containing_filename> ]] ; then
  while read line
    do
     :  
     # do whatever
    done < <variable_containing_filename>
fi

Like this?

read -p "entrer Liens Video ou le nom d'un fichier contenant des liens:" vid
            #Download Video
          if [[ -e $vid ]] ; then
             while read line
             do
                python vid-xml-decoder/ultimate.py $line
                # rest of video processing steps
             done < $vid
                else
                  python vid-xml-decoder/ultimate.py $vid
                   # rest of video processing steps
fi
0
 

Author Comment

by:Gaaara
ID: 40351882
sorry I do not understand I'm a beginner
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40351885
If you're a beginner you should perhaps start with less complex scripts.

What exactly don't you understand? Please reread my above comment, I added some stuff.
0
 

Author Comment

by:Gaaara
ID: 40351901
sorry is ok  i test :)

not fully works

Download complete

ffmpeg version 0.8.16-6:0.8.16-1, Copyright (c) 2000-2014 the Libav developers
  built on Sep 16 2014 23:10:48 with gcc 4.7.2
The ffmpeg program is only provided for script compatibility and will be removed
in a future release. It has been deprecated in the Libav project to allow for
incompatible command line syntax improvements in its replacement called avconv
(see Changelog for details). Please use avconv instead.
.flv: No such file or directory
mkvmerge v5.6.0 ('Kenya Kane') built on May 28 2012 06:47:39
Error: The source file '.ass' could not be opened successfully, or retrieving its size by seeking to the end did not work.

Open in new window


the step of muxing video not works

it should detect the names of video files and the .ass as my script

     find . -name "*.flv" | while read line
do          
            name="$(basename "${line}" .flv)"

Open in new window

0
 

Author Comment

by:Gaaara
ID: 40352424
not fully works
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40352437
Please post the (full) code you used.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:Gaaara
ID: 40352462
read -p "entrer Liens Video ou le nom d'un fichier contenant des liens:" vid
            #Download Video
          if [[ -e $vid ]] ; then
             while read line
             do
                python vid-xml-decoder/ultimate.py $line
               #Variable de déplacement de fichier  
                repExport="./export/"
                #mux des fichier
                ffmpeg -i "${name}.flv" -vcodec copy -acodec copy mkvtemp.mkv
                mkvmerge -v -o "${repExport}${name}.mkv" --default-track 0 --language 0:fre "${name}.ass" mkvtemp.mkv && \
             done < $vid
                else
                  python vid-xml-decoder/ultimate.py $vid
                   # rest of video processing steps
fi 

Open in new window

0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40352524
Is that really the full code??
Where does the variable "$name" get filled? Your script cannot work as long as this variable is empty.
You should integrate the posted "find" part into your script, like

 if [[ -e $vid ]] ; then
             while read line
             do
                python vid-xml-decoder/ultimate.py $line
               #Variable de déplacement de fichier  
                repExport="./export/"
                find . -name "*.flv" | while read line
                   do          
                       name="$(basename "${line}" .flv)"
                       ffmpeg ...
                       mkvmerge
                 done
        done < $vid
fi

The above is only a wild guess, because I'm not familiar at all with ffmpeg or mkvmerge etc.

Note: A manually entered url requires the same processing as an url pulled from a file.
So instead of my line "# rest of video processing steps" there must be  the same stuff which is already inside the "while" loop, i.e. not only "python vid= ..." but also the following lines up to "mkvmerge ...", including the "find" loop.

The double ampersand and the following backslash at the end of the "mkvmerge" line are wrong!
0
 

Author Comment

by:Gaaara
ID: 40352556
is just a code test :)  

for the variable  
     find . -name "*.flv" | while read line
         
            name="$(basename "${line}" .flv)"

Open in new window


is 2 line on my old script giving information for the flv and ass files :)

i test to intégrate this line no successful

"you speek french ?"
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40352631
Maybe it's a test, but I can't help you without full knowledge of the code used.
In fact, $name is NOT filled, see the error message from ffmpeg.

Français? Oui, j'en parle (au moins un peu), mais voici un site anglophone, so let's continue in English.
0
 

Author Comment

by:Gaaara
ID: 40352733
my old script works

#!/bin/bash
# Bash Menu

clear

echo "dl master"

PS3='entrez votre chois: '
options=("Creation du cookies.txt" "Téléchargement de la video" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Creation du cookies.txt")
             python vid-xml-decoder/login.py 1$
            break
            ;;
        "Téléchargement de la video")
              read -p "entrer Liens Video :" vid
              python vid-xml-decoder/ultimate.py $vid
              
             repExport="./export/"

             find . -name "*.flv" | while read line
           do
             name="$(basename "${line}" .flv)"
             echo "${name}"
             ffmpeg -i "${name}.flv" -vcodec copy -acodec copy mkvtemp.mkv
             mkvmerge -v -o "${repExport}${name}.mkv" --default-track 0 --language 0:fre "${name}.ass" mkvtemp.mkv && \
             rm -rf "${name}".flv "${name}".ass mkvtemp.mkv
   
done


              
             break
            ;;
        "Quit")
            break
            ;;
        *) echo invalid option;;
    esac
done

Open in new window


is my old script
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 40352775
OK, this is my best guess:
#!/bin/bash
# Function Definition
function process_video {
python vid-xml-decoder/ultimate.py $1
              
             repExport="./export/"

             find . -name "*.flv" | while read line
           do
             name="$(basename "${line}" .flv)"
             echo "${name}"
             ffmpeg -i "${name}.flv" -vcodec copy -acodec copy mkvtemp.mkv
             mkvmerge -v -o "${repExport}${name}.mkv" --default-track 0 --language 0:fre "${name}.ass" mkvtemp.mkv && \
             rm -rf "${name}".flv "${name}".ass mkvtemp.mkv
   
           done
}

# Bash Menu
clear

echo "dl master"

PS3='entrez votre chois: '
options=("Creation du cookies.txt" "Téléchargement de la video" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Creation du cookies.txt")
             python vid-xml-decoder/login.py 1$
            break
            ;;
        "Téléchargement de la video")
              read -p "entrer Liens Video ou chemin/nom d'un fichier contenant des liens:" vid
              
              if [[ -e $vid ]] ; then
                 while read Line
                  do
                    process_video $Line
                  done < $vid 
                 else
                    process_video $vid
              fi 
             break
            ;;
        "Quit")
            break
            ;;
        *) echo invalid option;;
    esac
done

Open in new window

I fear this will be all I can do.
You will notice that I moved all the video processing to a function so the code won't appear twice in the script.
0
 

Author Closing Comment

by:Gaaara
ID: 40352832
a1 5 star good job tx bro !

juste one question
you have a suggestion for a software for developer bash php python ?
and site for learn this language
0
 

Author Comment

by:Gaaara
ID: 40369901
sorry  i have an error on yor script

startdl.sh: line 57: ac: command not found
startdl.sh: line 58: syntax error near unexpected token `done'
startdl.sh: line 58: `done'

I is not changing nothing :(
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40370237
Did you accidentally shorten "esac" to "ac", maybe in the course of copy-and-paste?
0
 

Author Comment

by:Gaaara
ID: 40372094
hello


I did not touch anything :)

finally it is correct :)
i change this

 
*) echo invalid option;;
    esac

Open in new window

to
*) echo "invalid option"
;;

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now