ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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!!
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
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.
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
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.
#!/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
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
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")
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
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
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.