LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-11-2016, 12:40 PM   #16
ryganys
LQ Newbie
 
Registered: Nov 2016
Posts: 9

Original Poster
Rep: Reputation: Disabled

Quote:
Originally Posted by grail View Post
Well I would be hard pressed to see how you would solve this without at least one 'if'? Either that or you have not properly explained the problem?

Maybe if you provided the original question it may help for the rest of us instead of trying to provide answers that appear to not be helping?
i may have not understand the question properly my self so i will post the original question, maybe that will make you understand better
 
1 members found this post helpful.
Old 11-11-2016, 12:44 PM   #17
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by ryganys View Post
i may have not understand the question properly my self so i will post the original question, maybe that will make you understand better
Per the LQ Rules, please 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 Google searches) and we'll do our best to help. Also, keep in mind that your instructor might also be an LQ member.
 
Old 11-11-2016, 12:48 PM   #18
ryganys
LQ Newbie
 
Registered: Nov 2016
Posts: 9

Original Poster
Rep: Reputation: Disabled
Original question

So the question says :
Your task is to write a script called process_files that will accept from the user as command line arguments a series of 3 strings representing file types.

Now if i understand correctly I must create some sort of command in the case the user types (process_file jpg), the script will only sort the jpg files while leaving the rest intact. No?
The thing is this part of the question is supposed to be found online or talk it out with our classmates, so i would apreciate any help!!

Last edited by ryganys; 11-11-2016 at 12:51 PM.
 
Old 11-11-2016, 12:52 PM   #19
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Positional parameters were mentioned above in an earlier post. What have you found in regards to how to use them?

Quote:
Originally Posted by ryganys View Post
return some information about these files to the user.
You can use the program "file" to determine the file type of individual files.
 
2 members found this post helpful.
Old 11-11-2016, 12:57 PM   #20
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Quote:
Originally Posted by ryganys View Post
requires to pass some line arguments into the script.

set a command into the script to make it execute specific lines instead of the whole script.
Here is a script that allows an argument of either 'runpart1' or 'runpart2' which in turn runs a single function inside the script rather than running the whole script.


[root@centos01 ~]# cat basher.sh
Code:
#!/bin/bash

function runpart1() {
 echo "ran function runpart1"
}

function runpart2() {
 echo "ran function runpart2"
}

if [[ "$1" == "runpart1" ]]; then
  runpart1;
fi

if [[ "$1" == "runpart2" ]]; then
  runpart2;
fi
And the output when run with the options:
Code:
[root@centos01 ~]# ./basher.sh runpart1
ran function runpart1
[root@centos01 ~]# ./basher.sh runpart2
ran function runpart2
 
Old 11-11-2016, 12:58 PM   #21
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Member Response

Quote:
Originally Posted by ryganys View Post
So the question says :
Your task is to write a script called process_files that will accept from the user as command line arguments a series of 3 strings representing file types.

Now if i understand correctly I must create some sort of command in the case the user types (process_file jpg), the script will only sort the jpg files while leaving the rest intact. No?
The thing is this part of the question is supposed to be found online or talk it out with our classmates, so i would apreciate any help!!
This is all fine. You were offered reference material and informed about examples where there are scripts using passing arguments, as well as a link to read about passing arguments. It seems as if you haven't read those, nor have you tried to modify your script in any manner to use passing arguments.

What I'm looking for is for you to read up, and really this is a small amount of required reading, or even perform a web search to find out how to use command line arguments or passing arguments in a bash script. It is a very fundamental property which I feel you absolutely should understand.

If the form of help you wish is pure examples and solutions posted, then we would be doing a very severe disservice for your education. Once again, you can web search for things like "bash script command line argument examples" and get pure examples, but if you don't understand them properly, then what good does it do you except to get you through this one assignment. The next assignment you may need to recall and understand some important details about passing arguments, and you may not if you just copy and ignore the meaning.

They start with a $ and they have numbers. If you wish to actually use bash scripting for anything, or be capable of taking next steps as the required assignments grow in complexity, this very fundamental thing should absolutely be understood by you.

Note also that it seems the point of this assignment is exactly to have you work with passing arguments, therefore your instructor wishes you to learn about that topic.

Last edited by rtmistler; 11-11-2016 at 01:00 PM.
 
3 members found this post helpful.
Old 11-11-2016, 02:53 PM   #22
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So, now that we have the question, my first one back would be ... what is the zip file about? Nowhere in your question does it mention extracting files.

As mentioned above by others, most of what you need is easily found on the web or using any of the links provided by people here.

Based on the question provided, I would expect to see:

1. Test for arguments passed to script and appropriate message if the incorrect number have been supplied

2. An 'if' or 'case' set of clauses to process the arguments passed in with suitable output

3. Suitable exit statuses

The above would get you at least a passing mark with extra marks awarded for the efficiency of the code.

So let us know how you get on and as you can see, everyone is keen to help you get further, but you will have to put in some effort first
 
Old 11-11-2016, 03:05 PM   #23
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
I maintain a bash-script-skeleton to be able to create scripts quickly. It might be a good reference for you to help get you researching the correct things.

https://github.com/boardstretcher/ba...r/wireframe.sh
 
Old 11-11-2016, 04:00 PM   #24
ryganys
LQ Newbie
 
Registered: Nov 2016
Posts: 9

Original Poster
Rep: Reputation: Disabled
Script

#!/bin/bash
echo " Enter variable to proceed [ENTER]: "
read lol
if [ "$lol" = "gif" ];
then
cd /home/spiros/Downloads #this line changes directory to downloads

unzip unsorted_files.zip -d /home/spiros #this line unzips a file

mkdir /home/spiros/sorted_files #this line creates a directory
cd /home/spiros/sorted_files #this line changes directory to sorted files

mkdir jpg gif docx other #this line creates 3 new directories

cd /home/spiros/unsorted_files #this line changes directory to unsorted files

mv *gif /home/spiros/sorted_files/gif #this line moves all the content that ends in gif to a file called gif

mv * /home/spiros/sorted_files/other #this file moves everything in a file called other

echo "files have been sorted" #displays to terminal " files has been sorted "

cd /home/spiros/sorted_files/docx #changes directory to docx

touch list #creates list.txt

ls -s > list #lists and copies content in size order

cd /home/spiros/sorted_files/gif # changes directory to gif

touch list #creates list.txt

ls -s > list #lists and copies content in size order

cd /home/spiros/sorted_files/other #changes directory to other

touch list #creates list.txt

ls -s > list #lists and copies content in size order

cd /home/spiros/sorted_files/jpg #changes directory to jpg

touch list #creates list.txt

ls -s > list #lists and copies content in size order

source ~/Desktop/scripts/listmain > ~/sorted_files/label #runs script listmain
exit 1

elif [ "$lol" = "process_file_jpg" ];
then

cd /home/spiros/Downloads #this line changes directory to downloads

unzip unsorted_files.zip -d /home/spiros #this line unzips a file

mkdir /home/spiros/sorted_files #this line creates a directory
cd /home/spiros/sorted_files #this line changes directory to sorted files

mkdir jpg gif docx other #this line creates 3 new directories

cd /home/spiros/unsorted_files #this line changes directory to unsorted files
mv *jpg /home/spiros/sorted_files/jpg #this line moves all the content that ends in jpg to a file called jpg

mv * /home/spiros/sorted_files/other #this file moves everything in a file called other

echo "files have been sorted" #displays to terminal " files has been sorted "

cd /home/spiros/sorted_files/docx #changes directory to docx

touch list #creates list.txt

ls -s > list #lists and copies content in size order

cd /home/spiros/sorted_files/gif # changes directory to gif

touch list #creates list.txt

ls -s > list #lists and copies content in size order

cd /home/spiros/sorted_files/other #changes directory to other

touch list #creates list.txt

ls -s > list #lists and copies content in size order

cd /home/spiros/sorted_files/jpg #changes directory to jpg

touch list #creates list.txt

ls -s > list #lists and copies content in size order

source ~/Desktop/scripts/listmain > ~/sorted_files/label #runs script listmain
exit 1

elif [ "$lol" = "process_file_docx" ];
then
cd /home/spiros/Downloads #this line changes directory to downloads

unzip unsorted_files.zip -d /home/spiros #this line unzips a file

mkdir /home/spiros/sorted_files #this line creates a directory
cd /home/spiros/sorted_files #this line changes directory to sorted files

mkdir jpg gif docx other #this line creates 3 new directories

cd /home/spiros/unsorted_files #this line changes directory to unsorted files

mv *docx /home/spiros/sorted_files/docx #this line moves all the content that ends in docx to a file called docx

mv * /home/spiros/sorted_files/other #this file moves everything in a file called other

echo "files have been sorted" #displays to terminal " files has been sorted "

cd /home/spiros/sorted_files/docx #changes directory to docx

touch list #creates list.txt

ls -s > list #lists and copies content in size order

cd /home/spiros/sorted_files/gif # changes directory to gif

touch list #creates list.txt

ls -s > list #lists and copies content in size order

cd /home/spiros/sorted_files/other #changes directory to other

touch list #creates list.txt

ls -s > list #lists and copies content in size order

cd /home/spiros/sorted_files/jpg #changes directory to jpg

touch list #creates list.txt

ls -s > list #lists and copies content in size order

source ~/Desktop/scripts/listmain > ~/sorted_files/label #runs script listmain
exit 1

elif [ "$lol" = "process_file" ];
then
cd /home/spiros/Downloads #this line changes directory to downloads

unzip unsorted_files.zip -d /home/spiros #this line unzips a file

mkdir /home/spiros/sorted_files #this line creates a directory
cd /home/spiros/sorted_files #this line changes directory to sorted files

mkdir jpg gif docx other #this line creates 3 new directories

cd /home/spiros/unsorted_files #this line changes directory to unsorted files

mv *gif /home/spiros/sorted_files/gif #this line moves all the content that ends in gif to a file called gif

mv *jpg /home/spiros/sorted_files/jpg #this line moves all the content that ends in jpg to a file called jpg

mv *docx /home/spiros/sorted_files/docx #this line moves all the content that ends in docx to a file called docx

mv * /home/spiros/sorted_files/other #this file moves everything in a file called other

echo "files have been sorted" #displays to terminal " files has been sorted "

cd /home/spiros/sorted_files/docx #changes directory to docx

touch list #creates list.txt

ls -s > list #lists and copies content in size order

cd /home/spiros/sorted_files/gif # changes directory to gif

touch list #creates list.txt

ls -s > list #lists and copies content in size order

cd /home/spiros/sorted_files/other #changes directory to other

touch list #creates list.txt

ls -s > list #lists and copies content in size order

cd /home/spiros/sorted_files/jpg #changes directory to jpg

touch list #creates list.txt

ls -s > list #lists and copies content in size order

source ~/Desktop/scripts/listmain > ~/sorted_files/label #runs script listmain
exit 1

else
exit 1
echo "Incorect Variable"
fi

Okay i made a clear progress based on the code but when i run it, comes back with an error saying bad variable name lol.
After loads of search i found that every linux version might have diferent way to declare a variable, but i cant find the one for my version Linux mint.
Also another error is
process_file.sh: 96: process_file.sh: Syntax error: "elif" unexpected (expecting "then")

Last edited by ryganys; 11-11-2016 at 04:26 PM.
 
Old 11-11-2016, 10:20 PM   #25
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
As already mentioned several times in this thread, please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.

And as pointed out, you have been offered reference material and some very good tips in this thread, but you do not seem to have read it, or attempted to apply it.

Please show some effort to understand and use the knowledge on offer in a systematic way.

So let's break your task into smaller parts and begin with the very first part...

Quote:
Originally Posted by ryganys View Post
So the question says :
Your task is to write a script called process_files that will accept from the user as command line arguments a series of 3 strings representing file types.
So you need to understand how to pass command line arguments into your script before doing anything else. Do you know how to do that? Have you read the earlier post (#3 in this thread) about positional parameters?

You will find a complete example script here which will help you better understand how to process command line arguments. Understand how that works, then try to write into your own script only the parts necessary to echo your own command line arguments. From that point you can begin to work out how to use them in your script to accomplish your task.

So please try to work out this part of the task first, all the information you need to do so is available in this thread. Then we can more easily move on to the next part.

Last edited by astrogeek; 11-11-2016 at 10:22 PM. Reason: typos
 
1 members found this post helpful.
Old 11-12-2016, 09:59 AM   #26
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
In programming each time you write code that seems to do same thing, you usually can make a function / procedure and then just call this procedure instead of writing the same code again and again.

I mean for example:
Code:
create_sorted_list_file()
{
  cd /home/spiros/sorted_files/$1
  ls -s > list
}

...
...
create_sorted_list_file docx
create_sorted_list_file gif
create_sorted_list_file other
create_sorted_list_file jpg
 
Old 11-12-2016, 12:02 PM   #27
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Did I miss something? What was the solution?
 
Old 11-13-2016, 12:57 AM   #28
Jjanel
Member
 
Registered: Jun 2016
Distribution: any&all, in VBox; Ol'UnixCLI; NO GUI resources
Posts: 999
Blog Entries: 12

Rep: Reputation: 364Reputation: 364Reputation: 364Reputation: 364
Maybe OP edited #24 between 2:00&2:26, to fix error. ...Another 'unPC coverup'
I couldn't compose a script that gets that error. Huge 'extra credit' to LQ'er who does
Quote:
Syntax error: "elif" unexpected (expecting "then")

Last edited by Jjanel; 11-13-2016 at 12:59 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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script/Perl Script to remove the string until it finds special character '_' pooppp Programming 10 07-17-2012 09:36 AM
Shell script, Perl script, command or utility to convert Binary to text Perseus Programming 26 07-12-2012 06:00 AM
[SOLVED] bash and xterm: how make apps started by and for a script persist when script terminates porphyry5 Linux - General 4 06-15-2011 01:27 PM
[SOLVED] Script question: create a shell script in kde to log in on a server with ssh c4719929 Linux - Newbie 1 01-31-2011 03:05 AM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 08:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:07 AM.

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