Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

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

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
Gaaara
Asked:
Gaaara
  • 11
  • 9
1 Solution
 
woolmilkporcCommented:
while read url
   do
       echo $url
       ## do whatever with $url
   done < textfile
0
 
GaaaraAuthor Commented:
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
 
woolmilkporcCommented:
Where in that construct can we find the file containing the urls?
Where should the nested loop be placed?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
GaaaraAuthor Commented:
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
 
woolmilkporcCommented:
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
 
GaaaraAuthor Commented:
sorry I do not understand I'm a beginner
0
 
woolmilkporcCommented:
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
 
GaaaraAuthor Commented:
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
 
GaaaraAuthor Commented:
not fully works
0
 
woolmilkporcCommented:
Please post the (full) code you used.
0
 
GaaaraAuthor Commented:
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
 
woolmilkporcCommented:
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
 
GaaaraAuthor Commented:
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
 
woolmilkporcCommented:
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
 
GaaaraAuthor Commented:
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
 
woolmilkporcCommented:
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
 
GaaaraAuthor Commented:
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
 
GaaaraAuthor Commented:
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
 
woolmilkporcCommented:
Did you accidentally shorten "esac" to "ac", maybe in the course of copy-and-paste?
0
 
GaaaraAuthor Commented:
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 11
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now