LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-30-2021, 06:10 AM   #1
freewarehookie
LQ Newbie
 
Registered: Jul 2020
Location: on the moon
Distribution: Windows
Posts: 21

Rep: Reputation: Disabled
Shell scripting issue


Hello,

I am relatively new to Linux and I would like to know how you, as professionals, transcript the following command, which programs you use, to learn from it.

Unzip all files in a directory into the same name as the ZIP-File and save all found filenames, without the .zip, in a file called FILES. Each name shall have a new line.

What is the recommended command and way? To read out the directory first and all files in it, subtract the .zip and safe it in the FILES file with a specific loop and then use this names to create the directories with another loop and extract each file accordingly? How would you, as professionals, solve this issue in the most convenient and recommended manner?

How is it correct / by the book?

Would you create a FILES file for later reuse or would it be better to read out the already unpacked folders each time? Please answer with regard to programmers point of view.


Kind regards,

Freewarehookie

Last edited by freewarehookie; 11-30-2021 at 06:58 AM.
 
Old 11-30-2021, 07:08 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
Quote:
Originally Posted by freewarehookie View Post
What is the recommended command and way?
The obvious answer would be unzip, but in this particular case you may also consider using 7za instead (from package p7zip). It can handle ZIP format as well, allows the archive names to be fed from a list file, and its -o option can be used to extract archives to accordingly named subfolders.

Last edited by shruggy; 11-30-2021 at 10:22 AM.
 
1 members found this post helpful.
Old 11-30-2021, 07:15 AM   #3
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
Quote:
What is the recommended command and way? To read out the directory first and all files in it
Some basic examples:
Code:
echo *
ls

for i in *; do echo "$i"; sleep .1; done

ls | grep '.zip'

ls | grep '.zip' > files.txt

var1=$(while read line; do echo "$line"; done < files.txt)
echo "$var1"

dir=(*)
for i in "${dir[@]}"; do echo "$i"; sleep .1; done
See the man pages for:
man ls
man echo
man grep
man zip
man unzip
read --help
 
1 members found this post helpful.
Old 11-30-2021, 07:21 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
It looks like a homework (or similar) question. See LQ rules:
Quote:
Do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and searches) and we'll do our best to help. Keep in mind that your instructor might also be an LQ member.
I have no idea what do you mean by "programmers point of view" and what other views exist. There is no most convenient and recommended way, it always depend at least on the environment, the users.

Quote:
How is it correct / by the book?
I also don't understand what book is that?

Also would be nice to show an example about this issue.
 
1 members found this post helpful.
Old 11-30-2021, 09:19 AM   #5
freewarehookie
LQ Newbie
 
Registered: Jul 2020
Location: on the moon
Distribution: Windows
Posts: 21

Original Poster
Rep: Reputation: Disabled
detailed definition

Quote:
It looks like a homework (or similar) question. See LQ rules:
It's no homework. It's just an approach to learn Shell Script language and solutions of well educated programmers, who know how such a script should be made.

Quote:
I have no idea what do you mean by "programmers point of view" and what other views exist. There is no most convenient and recommended way, it always depend at least on the environment, the users.
You might program the same solution in different ways. I am searching for the correct one, regarding for example GNU/Linux standards. If they would use "tar" for this, it would be nice if mentioned in the post. Maybe "tar" is able to unzip .zip files as well and the code could be optimized to a precise minimum.

Lets say a professional programmer won't create a files.txt and if he wanted to know which were extracted would compare the already extracted .zip files (folder existant) against the .zip files to find .zip files, which haven't been extracted yet, to finish this task (let's say with a cron job). I just want to know what a well educated programmer would do.

Quote:
I also don't understand what book is that?
"By the book" is an english expression for a Professional's (studied) Person's solution. "By the book" = like written in a book for the education of programmers.


Kind regards,

Freewarehookie
 
Old 11-30-2021, 09:44 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,757

Rep: Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983
Quote:
Originally Posted by freewarehookie View Post
It's no homework. It's just an approach to learn Shell Script language and solutions of well educated programmers, who know how such a script should be made.
Sorry, it looks and reads like homework. You wouldn't just arbitrarily pick that wording/question out of the blue.
Quote:
You might program the same solution in different ways. I am searching for the correct one, regarding for example GNU/Linux standards. If they would use "tar" for this, it would be nice if mentioned in the post. Maybe "tar" is able to unzip .zip files as well and the code could be optimized to a precise minimum.

Lets say a professional programmer won't create a files.txt and if he wanted to know which were extracted would compare the already extracted .zip files (folder existant) against the .zip files to find .zip files, which haven't been extracted yet, to finish this task (let's say with a cron job). I just want to know what a well educated programmer would do.
Again, there is no 'correct one'. Every programmer will do things in their own way...if you ask 100 programmers, you'll likely get 100 different answers, and all of them will work.
Quote:
"By the book" is an english expression for a Professional's (studied) Person's solution. "By the book" = like written in a book for the education of programmers.
Yes, we know what it means, and it doesn't apply here. It is like asking "how high is up?" There is no single book with the 'correct' way of doing something for programming.

Since you claim this isn't homework, why don't you post what YOU have done/tried so far to accomplish this task? You have been given hints, man pages to read, and there are abundant bash scripting tutorials you can find with a simple search. Read the LQ Rules about posting homework, and the "Question Guidelines". Whether or not you're an experienced coder is immaterial; we aren't going to write your scripts for you, or do your homework for you.
 
Old 11-30-2021, 09:59 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
no we have no standards for that. there are a lot of different ways to solve it. For example if you prefer python you can do it in pure python (or perl, or java or ...).
You can use tar to unpack/unzip file, but you can use 7zip or other tools too.
What is your goal? Do you want us to solve it? (actually I don't need this tool, but I may help you to implement it).
 
Old 11-30-2021, 10:06 AM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
This is one of those questions (at least IMHO) where if you ask 5 people you would get 6 different answers. It probably does not matter how efficient but whether it is readable and you understand how it works. But I what do I know...

Without knowing exactly whats in the zip file it depends on how the zip file was created and if it contains any path information. If you just unzip the file you could be creating one or a number of sub-directories. As suggested you could output the list of archived files within the zip file to a text file without extracting. From there you can create a destination directory and unzip to that directory.
 
Old 11-30-2021, 10:15 AM   #9
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
Quote:
Originally Posted by freewarehookie View Post
If they would use "tar" for this, it would be nice if mentioned in the post. Maybe "tar" is able to unzip .zip files as well
No, GNU tar cannot unpack ZIP files. But bsdtar can. It's not the default tar on Linux, but can be installed on many Linux distributions: look either for package bsdtar (most RPM-based distros) or libarchive-tools (Debian-based) or libarchive (Arch and Slackware).
 
Old 11-30-2021, 11:14 AM   #10
freewarehookie
LQ Newbie
 
Registered: Jul 2020
Location: on the moon
Distribution: Windows
Posts: 21

Original Poster
Rep: Reputation: Disabled
well

I thought there would be a standard solution like this:

under GNU/Linux there is the ZIP and UNZIP tool and if you don't need to create files you shouldn't do it. You should use a while loop, to repeat it as long as files exist,
where you ask wether a folder corresponding to the .zip file exists or not and if it exists, you don't do anything and if it doesn't exist, you use the unzip command, that's it.
Its the shortest way to do it, just write it into a .sh file and install it as a cronjob.
If you copy a .zip file into the folder, it will automatically be unpacked as soon as the .sh file is is initialized through the cronjob.

Hope to have helped you out.

...

But if it's not like this, that might be the answer to my post.

Thanks a lot.

I will order and read a book about shell scripting now.


Kind regards,

Freewarehookie
 
Old 11-30-2021, 01:08 PM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
You know I have no idea how is it related to cron, but I'm sure it is suitable for you. That's why there is no common way.
 
Old 11-30-2021, 03:06 PM   #12
freewarehookie
LQ Newbie
 
Registered: Jul 2020
Location: on the moon
Distribution: Windows
Posts: 21

Original Poster
Rep: Reputation: Disabled
just an example

cron was just an example to use the script regularly.
Wget as cron could download the latest stable release of a program and if the file is a new one it would then automatically be unpackt and compiled and installed...

...

as an example
 
Old 11-30-2021, 03:26 PM   #13
freewarehookie
LQ Newbie
 
Registered: Jul 2020
Location: on the moon
Distribution: Windows
Posts: 21

Original Poster
Rep: Reputation: Disabled
main problem

In addition to zip/unzip/tar: I think one major problem is, that there is no list of "standard" programs for GNU/Linux, lets say for example Richard Stallman himself recommends.

My problem is, that I do not have a list of standard GNU/Linux Tools and as you can see in this post, there are a whole bunch of different varieties for the same program, which
exactly does the same, but in dependance of different Distributions, there are different tools that evolved.

Can't somebody standardize that? I don't need 10 different zip Tools.

I will use LFS and BLFS, they seem to use standard tools, but what I am really searching for is an old Linux crack, who uses the most reliable and maybe oldest tools, which still
work fine and have evolved over centuries.

An Example: For the x-window-system there are at least 6 different window managers, in Windows named "file explorers".

(This is the explanation for one of the intentions of this topic at the beginning)


Kind regards,

Freewarehookie

Last edited by freewarehookie; 11-30-2021 at 03:29 PM.
 
Old 12-01-2021, 02:03 AM   #14
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
Quote:
Originally Posted by freewarehookie View Post
Can't somebody standardize that? I don't need 10 different zip Tools.
The main goal is the freedom: you can find your own way and approach. You are not forced to use the one and only savior solution.
 
1 members found this post helpful.
Old 12-01-2021, 03:10 AM   #15
freewarehookie
LQ Newbie
 
Registered: Jul 2020
Location: on the moon
Distribution: Windows
Posts: 21

Original Poster
Rep: Reputation: Disabled
I fully understand this approach

Dear pan64,

I fully understand this approach, I am with you!

But people, who are new and try to build a standardized system with the most important tools, which have been used for decades and are, lets say the "original" tools, because in former days there weren't so many, are getting really really fed up with Linux before they even started!

And that's what I call: stupid! Because you want to let people enjoy building a Linux System, you want those people to be passionate about this wonderful open source system and you don't want to annoy them or steal the fun with it, before they even have begun!

That's the main aspect related to the variety of programs.

With relation to programming: Yes, that's correct. But if you are a good programmer, you might want to find an efficient solution for a problem. Can we agree on that?


Kind regards,

Freewarehookie

Last edited by freewarehookie; 12-01-2021 at 03:12 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Shell Scripting Part I: Getting started with bash scripting LXer Syndicated Linux News 0 04-29-2015 08:03 AM
win32,shell code,shell programming,shell scripting? mr.cracker Linux - Newbie 4 07-12-2013 11:20 PM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 03:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration