Skip to main content
deleted 18 characters in body
Source Link
Rui F Ribeiro
  • 58k
  • 28
  • 156
  • 237

If your textfile is strict, as in your group will always be called "Group" followed by a space and a word and will not have any spaces in the group name, and similarly for the names after .html, this should work.

#!/bin/bash

parseNdostuff(){
    case $1 in
        Group)
            shift
            mkdir $1
            cd $1
            ;;
        *.html)
            link=$1
            shift
            echo $link > $1
            ;;
        *)
            cd ..
            ;;
    esac
        
}

while read line
do
   parseNdostuff $line
done <  mytext.txt

Just do a while read line and your text file input at the end < mytext.txt, and feed it to a function i wrote. The reason I chose to do it in a function was for simplicity of dealing with each line, plus being able to easily use case and shift.

Some details, if you're noob...:

$1 is the first argument of the function (the $ variables are special, look them up)

shift pushes down the arguments ($1 gets dropped, $2 becomes $1, $3 -> $2)

when it creates a directory it cd into that directory so when the statement runs again and sees the .html lines it makes the files and inputs the link all in the proper directory.

*) is all other cases, so if you have a blank line between groups, it will just cd .. and go down a directory, and you'll be back where you started your script. Note, all these cd's are relative to your script placement.

If your textfile is strict, as in your group will always be called "Group" followed by a space and a word and will not have any spaces in the group name, and similarly for the names after .html, this should work.

#!/bin/bash

parseNdostuff(){
    case $1 in
        Group)
            shift
            mkdir $1
            cd $1
            ;;
        *.html)
            link=$1
            shift
            echo $link > $1
            ;;
        *)
            cd ..
            ;;
    esac
        
}

while read line
do
   parseNdostuff $line
done <  mytext.txt

Just do a while read line and your text file input at the end < mytext.txt, and feed it to a function i wrote. The reason I chose to do it in a function was for simplicity of dealing with each line, plus being able to easily use case and shift.

Some details, if you're noob...

$1 is the first argument of the function (the $ variables are special, look them up)

shift pushes down the arguments ($1 gets dropped, $2 becomes $1, $3 -> $2)

when it creates a directory it cd into that directory so when the statement runs again and sees the .html lines it makes the files and inputs the link all in the proper directory.

*) is all other cases, so if you have a blank line between groups, it will just cd .. and go down a directory, and you'll be back where you started your script. Note, all these cd's are relative to your script placement.

If your textfile is strict, as in your group will always be called "Group" followed by a space and a word and will not have any spaces in the group name, and similarly for the names after .html, this should work.

#!/bin/bash

parseNdostuff(){
    case $1 in
        Group)
            shift
            mkdir $1
            cd $1
            ;;
        *.html)
            link=$1
            shift
            echo $link > $1
            ;;
        *)
            cd ..
            ;;
    esac
        
}

while read line
do
   parseNdostuff $line
done <  mytext.txt

Just do a while read line and your text file input at the end < mytext.txt, and feed it to a function i wrote. The reason I chose to do it in a function was for simplicity of dealing with each line, plus being able to easily use case and shift.

Some details:

$1 is the first argument of the function (the $ variables are special, look them up)

shift pushes down the arguments ($1 gets dropped, $2 becomes $1, $3 -> $2)

when it creates a directory it cd into that directory so when the statement runs again and sees the .html lines it makes the files and inputs the link all in the proper directory.

*) is all other cases, so if you have a blank line between groups, it will just cd .. and go down a directory, and you'll be back where you started your script. Note, all these cd's are relative to your script placement.

Source Link
Joseph Glover
  • 353
  • 1
  • 2
  • 9

If your textfile is strict, as in your group will always be called "Group" followed by a space and a word and will not have any spaces in the group name, and similarly for the names after .html, this should work.

#!/bin/bash

parseNdostuff(){
    case $1 in
        Group)
            shift
            mkdir $1
            cd $1
            ;;
        *.html)
            link=$1
            shift
            echo $link > $1
            ;;
        *)
            cd ..
            ;;
    esac
        
}

while read line
do
   parseNdostuff $line
done <  mytext.txt

Just do a while read line and your text file input at the end < mytext.txt, and feed it to a function i wrote. The reason I chose to do it in a function was for simplicity of dealing with each line, plus being able to easily use case and shift.

Some details, if you're noob...

$1 is the first argument of the function (the $ variables are special, look them up)

shift pushes down the arguments ($1 gets dropped, $2 becomes $1, $3 -> $2)

when it creates a directory it cd into that directory so when the statement runs again and sees the .html lines it makes the files and inputs the link all in the proper directory.

*) is all other cases, so if you have a blank line between groups, it will just cd .. and go down a directory, and you'll be back where you started your script. Note, all these cd's are relative to your script placement.