Im not understanding script. Please someone..

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
lenmanny

Im not understanding script. Please someone..

Post by lenmanny »

I have read a lot of shell script tutors off of google,
and linux mint forums. They say there easy, but am
having a lot of trouble understanding them. I have ADHD.
My reading, writing, and consintration is very bad. I
was wondering, if someone can rewrite this run.bat file
into a linux shell script file for me. I will be double
clicking on the file to run it from a file manager.

The files name is run.bat...
Inside the file is only one line...

unrar e file_1.rar

Can someone turn that one line into a shell script file
please?

Also, i have a multi part rar file...

file_2.part1.rar
file_2.part2.rar
file_2.part3.rar
file_2.part4.rar

Will that same command (unrar e file.rar) unrar
multipart file?

Also, that file_1.rar was made in windows. It has a
windows folder in it called temp. Inside that temp
folder is all the files. Will that command (unrar e
file_1.rar) keep the original structure?

I mite have to have two different run.bat files...

run_1.bat
run_2.bat

And by the way..both files are password protected.

Can someone write these scripts for me? With me having
ADHD, i am having a hard time understanding the easy
tutors on google and mint forms. They are making no
since to me.

I do no a lot on how to use file managers. I do no a lot
about windows folders, how to create them, how to move
around them, how to copy, cut, paste, rename and a lot
of other file managment stuff.

I just need to work with rar files in linux from a file
manager. And the easyest way for me to do that is by
unraring a rar file by double clicking on an shell
script file. The same way you would with an run.bat
file.

So will someone write a couple simple shell script files
to unrar these files for me? Please?

Thanks a Lot
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
AlanWalker

Re: Im not understanding script. Please someone..

Post by AlanWalker »

The Mint community is largely a community of people new to Linxu so, while there are a few here experienced in writing shell scripts, those who have done such things are few in number.

Your script would have to ask where your RAR files are, and where you want the output put.

You mention that you are comfortable in work with directories inside a file manager. Good, you should be able to move files around and know where they are. Once you can do that you have the ability to open a terminal in the directory where your RAR files are and execute unrar filename.rar .

Why would that not work for you?

Regards,
Last edited by xenopeek on Tue May 27, 2014 11:38 am, edited 1 time in total.
Reason: pointless quote of entire post above removed
User avatar
xenopeek
Level 25
Level 25
Posts: 29597
Joined: Wed Jul 06, 2011 3:58 am

Re: Im not understanding script. Please someone..

Post by xenopeek »

Well, that's easy :)

Code: Select all

#!/bin/bash
unrar e file_1.rar
That's the entire script. The first line is mandatory; marks this as a Bash script. You have to then right-click the file, go to Properties, and in Permissions set to allow executing the file as a program.

But probably if you double-click your rar file from your file manager, it will already open it?
Image
User avatar
tdockery97
Level 14
Level 14
Posts: 5058
Joined: Sun Jan 10, 2010 8:54 am
Location: Mt. Angel, Oregon

Re: Im not understanding script. Please someone..

Post by tdockery97 »

Or you can do it the lazy way like me. Right-click the RAR file and select "Extract Here". :lol:
Mint Cinnamon 20.1
lenmanny

Re: Im not understanding script. Please someone..

Post by lenmanny »

xenopeek wrote:Well, that's easy :)

Code: Select all

#!/bin/bash
unrar e file_1.rar
That's the entire script. The first line is mandatory; marks this as a Bash script. You have to then right-click the file, go to Properties, and in Permissions set to allow executing the file as a program.

But probably if you double-click your rar file from your file manager, it will already open it?
Thanks for telling me that the first line is aways
important in a shell script. i still am having a hard
time getting it to click in my head because of ADHD, but
i've got the shell script figured out now. i've created
two different shell scripts that i will show everyone
how to create. i will post below. Thanks.
lenmanny

Re: Im not understanding script. Please someone..

Post by lenmanny »

tdockery97 wrote:Or you can do it the lazy way like me. Right-click the RAR file and select "Extract Here". :lol:
Some rar files you download off of the internet are made
on windows. I was downloading TV episodes. One out of
five rar files would not extract using archive manager
in linux. I would get (i forgot what the error was) an
error. Thats one out of five rar files that was created
on windows. So for me to get that one out of five rar
file extracted, I would have to type in terminal..

unrar e filename.rar

..to get that one out of five rar file extracted. It was
just taking too much time to do an extraction that way
sence i had so many tv episodes to extract.

Anyway, i did figure out how to create a couple of bash
shell scripts to speed up the process. I thank xenopeek
for showing me a simple script and the rest i figured
out my self.

Next post down i will show how i created two simple bash
shell scripts.
lenmanny

[SOLVED] Im now understanding unrar script. Thanks.

Post by lenmanny »

Thanks again xenopeek for showing me a simple script.

Heres how to create 2 different shell scripts to unrar
rar files.

1. Create 2 document files named..

Unrar_File.sh

and

Unrar_Multi-Part_File.sh

2. Put these 2 files in the same directory that your rar
or multi-part rar file is.

3. Open the Unrar_File.sh file with a txt editor like
the editor called pluma.

4. Type these lines in the file..

#!/bin/bash
unrar x *.rar

..and save the Unrar_File.sh file and exit your txt
editor.

But, if you want your rar file to extract to a certain
directory, the second line should look like this..

unrar x *.rar /home/your user name/name of directory

your user name = the user name you used when you
installed linux.

name of directory = the name of the directory you want
the rar file extracted too. For example, here is what i
typed in for my linux computer..

unrar x *.rar /home/lenmanny/episodes

5. Open the Unrar_Multi-Part_File.sh file with a txt
editor like the editor called pluma.

6. Type these lines in the file..

#!/bin/bash
unrar x *.part1.rar

..and save the Unrar_Multi-Part_File.sh file and exit
your txt editor.

But, if you want your rar file to extract to a certain
directory, the second line should look like this..

unrar x *.part1.rar /home/your user name/name of
directory

your user name = the user name you used when you
installed linux.

name of directory = the name of the directory you want
the rar file extracted too. For example, here is what i
typed in for my linux computer..

unrar x *.part1.rar /home/lenmanny/episodes

7. Now that you have your shell script files created,
and put in the directory where your rar file is, theres
one more thing you have to do...one at a time, right-
click on the files you created, go to properties, in the
Permissions tab, put a check mark in the allow executing
the file as a program.

8. To extract a rar file with one of the two shell
script files you created, just double-click the script
file, choose run terminal, and thats it. The rar file
will extract.
tadaensylvermane
Level 2
Level 2
Posts: 88
Joined: Tue Jun 25, 2013 8:50 pm

Re: Im not understanding script. Please someone..

Post by tadaensylvermane »

Code: Select all

#!/bin/bash

[ ! -d $2 ] && mkdir -p $2
find $1 -iname "*.rar" -exec unrar -x {} $2 \;
the first line is probably more clear like this.

Code: Select all

if [ ! -d $2 ] ; then
    mkdir -p $2
fi
Where $1 is the name of the directory where the Rar files are located. $2 is the path to where you want them extracted. This will create the folder to extract to if it isn't there. Example usage below...

Code: Select all

./unrarscript ~/rarfiles ~/rarextract
AlanWalker

Re: Im not understanding script. Please someone..

Post by AlanWalker »

tadaensylvermane wrote:

Code: Select all

#!/bin/bash

[ ! -d $2 ] && mkdir -p $2
find $1 -iname "*.rar" -exec unrar -x {} $2 \;
the first line is probably more clear like this.

Code: Select all

if [ ! -d $2 ] ; then
    mkdir -p $2
fi
Where $1 is the name of the directory where the Rar files are located. $2 is the path to where you want them extracted. This will create the folder to extract to if it isn't there. Example usage below...

Code: Select all

./unrarscript ~/rarfiles ~/rarextract
Hi,

Interesting stuff!

Would you please explain how bash interprets/executes [ ! -d $2 ] and {} $2 \; in your first code:select all?

TIA
tadaensylvermane
Level 2
Level 2
Posts: 88
Joined: Tue Jun 25, 2013 8:50 pm

Re: Im not understanding script. Please someone..

Post by tadaensylvermane »

AlanWalker wrote:
tadaensylvermane wrote:

Code: Select all

#!/bin/bash

[ ! -d $2 ] && mkdir -p $2
find $1 -iname "*.rar" -exec unrar -x {} $2 \;
the first line is probably more clear like this.

Code: Select all

if [ ! -d $2 ] ; then
    mkdir -p $2
fi
Where $1 is the name of the directory where the Rar files are located. $2 is the path to where you want them extracted. This will create the folder to extract to if it isn't there. Example usage below...

Code: Select all

./unrarscript ~/rarfiles ~/rarextract
Hi,

Interesting stuff!

Would you please explain how bash interprets/executes [ ! -d $2 ] and {} $2 \; in your first code:select all?

TIA
I probably am not correct, just know that they work.

Code: Select all

[ ! -d $2 ]
Is a test on a directory ( the -d flag. In this case with the ! it returns true if the directory $2 doesn't exist. As above if it returns true ( && ) then it makes the directory specified. Typically in my experience these tests are always some type of if statement. This is just a more concise, yet clearly less understandable method of doing an if test. Example
This...

Code: Select all

[ -d $1 ] && [ ! -d $2 ] ;
rm -r $1 && mkdir $2
is the same as this.

Code: Select all

if [ -d $1 ] ; then
    if [ ! -d $2 ] ; then
         rm -r $1
         mkdir $2
    fi
fi

Code: Select all

{} $2 \;
This one is easier. The {} is basically a variable. That variable comes from the find command. In my use above I use find instead of a for loop. Again for simplification although a bit less clear again. I just like things as compact as possible. Less lines the better. The \; is just required after the -exec (command) part of the find. It tells find that is the end of the line of what to do or something. I don't really understand the proper meaning, just know that's what it does.

Code: Select all

find $1 -iname "*.rar" -exec unrar -x {} $2 \;
is the exact same as this...

Code: Select all

for i in "$(ls -l ${1})" ; do
    unrar -x $i $2
done
I'm not sure I have that for statement correct but the idea is there. Again a single line is easier... at least for me to manage and maintain. Also I use find because more than once it has been brought to my attention that using ls in a script should be avoided. I never understood the reasons but it kept coming up. I don't use it anymore unless I have no choice.
AlanWalker

Re: Im not understanding script. Please someone..

Post by AlanWalker »

tadaensylvermane wrote:
AlanWalker wrote:
tadaensylvermane wrote: This...

Code: Select all

[ -d $1 ] && [ ! -d $2 ] ;
rm -r $1 && mkdir $2
is the same as this.

Code: Select all

if [ -d $1 ] ; then
    if [ ! -d $2 ] ; then
         rm -r $1
         mkdir $2
    fi
fi
<snip>
I'm not sure I have that for statement correct but the idea is there. Again a single line is easier... at least for me to manage and maintain. Also I use find because more than once it has been brought to my attention that using ls in a script should be avoided. I never understood the reasons but it kept coming up. I don't use it anymore unless I have no choice.
I am the same way about concise code.

I am interested in regex because I see it used so much in examples of the use of tar and I'm looking into tar.

I've found that there are at least two sites, here and here, that will help me learn to use regex but, thinking that your reply would possibly be of additional help to me, I'd like to ask 'What drove you to learn regex, and how did you learn?'.
tadaensylvermane
Level 2
Level 2
Posts: 88
Joined: Tue Jun 25, 2013 8:50 pm

Re: Im not understanding script. Please someone..

Post by tadaensylvermane »

I've tried just "reading a book" a few times. I have an older edition "Learning the bash Shell" and have read a bunch of http://www.tldp.org/LDP/abs/html/ . I learn by example primarily. So generally I've either asked or googled how to do a given thing, then I take what I find and tear it apart until I have a basic understanding of it. I can't quote the actual terminology ( didn't know what regex meant until you asked and I looked it up ) but I can use it to a reasonable degree and am only getting more and more figured out every day. I get a bunch of advice and suggestions on http://www.overclock.net/ linux section as well. I post there often.
AlanWalker

Re: Im not understanding script. Please someone..

Post by AlanWalker »

You've been good help.

Thank you,
tadaensylvermane
Level 2
Level 2
Posts: 88
Joined: Tue Jun 25, 2013 8:50 pm

Re: Im not understanding script. Please someone..

Post by tadaensylvermane »

Code: Select all

[ -d $1 ] && [ ! -d $2 ] ;
rm -r $1 && mkdir $2
Someone pointed out something over on LQs and I am posting hoping that someone notices and doesn't follow my bad example. The above does NOT work. It will always execute the action regardless of the outcome of the 2 tests. The only way for this to work is as such...

Code: Select all

[ -d $1 ] && [ ! -d $2 ] && rm -r $1 && mkdir $2
This works, tested with echos and such. I apologize for my own ignorance. I clearly misunderstood when I learned how that worked.
Locked

Return to “Scripts & Bash”