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.
Hello guys, i really need some help.
I have been developing a linux script for uni and im stuck at a question where it requires to pass some line arguments into the script.
So i would like to know how can set a command into the script to make it execute specific lines instead of the whole script.
As you have not really provided much information or examples, I will simply advise you need to look at creating functions. If this is not an adequate solution, please provide more details?
Hi ryganys and welcome to LQ. Please review this link for some tips on better ways to form your questions.
As others have said, you need to show some work here and post your initial attempts.
The My Bash Blog link in my signature illustrates how to use passing arguments somewhat and there are some examples throughout that of scripting as well as a lot of debugging hints.
There are also links for other Bash guides, both starting and advanced.
This the script i have been developing... I know its not much but its my 1st script!!
The part i need help with is for me to be able to specify which file the script is going to sort
smthing like process_file gif
Im actually stuck there, i have taken no further actions..
I recommend a few first steps here:
In the future, post your code directly into your replies, using the [code][/code] BB codes, or in Advanced Edit mode, paste your code, select it, and click on the # icon at the top of the Advanced edit window to wrap the code in the code BB codes. This will make it better tor read.
Add the line:
Code:
#!/bin/bash
as the first line of your script.
As the second line of your script, consider adding the line:
Code:
set -xv
This will enable debug and allow you to see the operation of each line of code when you run your script
Have you run this script already and found it to not work, or found unexpected results? What does happen when you run the script? Or have you not yet tried to run it? Do you need assistance with how to run the script?
Once again, there are debugging recommendations in my bash blog.
Please review bash references, but also please post a bit more about what does and doesn't work on your script.
In the future, post your code directly into your replies, using the [code][/code] BB codes, or in Advanced Edit mode, paste your code, select it, and click on the # icon at the top of the Advanced edit window to wrap the code in the code BB codes. This will make it better tor read.
Add the line:
Code:
#!/bin/bash
as the first line of your script.
As the second line of your script, consider adding the line:
Code:
set -xv
This will enable debug and allow you to see the operation of each line of code when you run your script
Have you run this script already and found it to not work, or found unexpected results? What does happen when you run the script? Or have you not yet tried to run it? Do you need assistance with how to run the script?
Once again, there are debugging recommendations in my bash blog.
Please review bash references, but also please post a bit more about what does and doesn't work on your script.
I don't see any obvious errors with your syntax.
The problem is not an error or something but adding extra features.
you see when you source the script its going to sort all files, but what if i want to make a command such as *source process_file gif* in order to process only the files ending with .gif .
I cant explain it better, im sorry.
The problem is not an error or something but adding extra features.
you see when you source the script its going to sort all files, but what if i want to make a command such as *source process_file gif* in order to process only the files ending with .gif .
I cant explain it better, im sorry.
You don't have to make a different script for only gif files, you can pass an argument to your script. You do not need to source the script, you can just run it. Likely it is executable permissions already, but if not, then use the chmod command to make your script executable. You can then run it by typing the name of the script, providing you are in the same directory as it, or the directory containing the script resides within your path environment variable.
When you pass an argument to your script, it is accessible as a variable within the script, see section 3.2.5 http://www.tldp.org/LDP/Bash-Beginne...tml#sect_03_02. You can use a word or a number, test the passed argument, and then choose different paths within your script, depending on the passed argument. In your case, if you pass "gif" or "jpg" you can use that string as the extension to manage what types of files you wish to process within your script.
Currently your script is a simple sequence of tasks and will follow the same list every time as is. May I suggest, prior to taking the advice above, you look at how you might
reduce the script down to its core components.
Here are some tips:
1. You do not need to be in a particular directory to use it, ie. there is no need to change (cd) into each directory to perform tasks if you provide the path to where the tasks should complete their objectives
2. Don't assume things about your environment.
a - With regard to unzipping your file, you assume there is now a directory with the correct name to be used (what if the unzipping errors, what if the unzipped item(s) is not a directory, what if the directory has a different name).
b - You touch and use mkdir with no regard for the fact that the items (files or directories) may already exist
c - You use "mv *" at one point, what if there are other types of files such as directories or symbolic links? Should they all be moved? Again, some checking is probably required
d - You move certain type of file (gif,jpg,etc) but again no testing that any of these actually exist in the first place
A number of the items listed above will result in errors which the user will not be provided with any information about, apart from the system default, nor any way to recover from said errors
I would go back and look at some of these before you worry too much about how to tell your script to only work on a subset of the files to be addressed.
Currently your script is a simple sequence of tasks and will follow the same list every time as is. May I suggest, prior to taking the advice above, you look at how you might
reduce the script down to its core components.
Here are some tips:
1. You do not need to be in a particular directory to use it, ie. there is no need to change (cd) into each directory to perform tasks if you provide the path to where the tasks should complete their objectives
2. Don't assume things about your environment.
a - With regard to unzipping your file, you assume there is now a directory with the correct name to be used (what if the unzipping errors, what if the unzipped item(s) is not a directory, what if the directory has a different name).
b - You touch and use mkdir with no regard for the fact that the items (files or directories) may already exist
c - You use "mv *" at one point, what if there are other types of files such as directories or symbolic links? Should they all be moved? Again, some checking is probably required
d - You move certain type of file (gif,jpg,etc) but again no testing that any of these actually exist in the first place
A number of the items listed above will result in errors which the user will not be provided with any information about, apart from the system default, nor any way to recover from said errors
I would go back and look at some of these before you worry too much about how to tell your script to only work on a subset of the files to be addressed.
I have noticied all of the above that you menrioned bur our system is a closed system wich means that the zip file has that one name and none of the files already excist, but thanks for the advice.
So far the script works as it should, even though its not very robust bu it works.
It may work, but if I am understanding your new requirements, it cannot be easily modified.
I would add that in your original post you mention you are at University, so i am not exactly sure how you would consider it a closed system? Are you saying that no one else on the campus but you is able to access it? Also, if you are testing your script, then the files would already exist as nowhere in your script are the directories created removed.
If you are in a course and learning about linux / scripting, most of the information I provided would be a minimum for getting a part way decent grade.
However, if you are leaving the code as is, I would suggest you wrap all the existing code into an 'if' and then in the else portion you can perform the tasks for when you are providing arguments to the script.
It may work, but if I am understanding your new requirements, it cannot be easily modified.
I would add that in your original post you mention you are at University, so i am not exactly sure how you would consider it a closed system? Are you saying that no one else on the campus but you is able to access it? Also, if you are testing your script, then the files would already exist as nowhere in your script are the directories created removed.
If you are in a course and learning about linux / scripting, most of the information I provided would be a minimum for getting a part way decent grade.
However, if you are leaving the code as is, I would suggest you wrap all the existing code into an 'if' and then in the else portion you can perform the tasks for when you are providing arguments to the script.
In order to opperate a uni computer u have to login with your credentials but after you log out everthing that you changed are deleted, which means you can only store them at an external usb.
I thought about the if method but i encountered so many errors which made me drop it as we have not worked on if methods in linux.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.