LinuxQuestions.org
Visit Jeremy's Blog.
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-10-2016, 09:03 PM   #1
ryganys
LQ Newbie
 
Registered: Nov 2016
Posts: 9

Rep: Reputation: Disabled
Post Script


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.
 
Old 11-10-2016, 10:43 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
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?
 
Old 11-10-2016, 10:58 PM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,337
Blog Entries: 3

Rep: Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732Reputation: 3732
You may also want to look at positional parameters if you are talking about using data that is passed along as run-time parameters.

Code:
./myscript foo
./myscript foo bar
./myscript foo bar baz
# etc.
 
Old 11-11-2016, 04:14 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,365

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
Please show us what you've tried so far, using CODE tags https://www.linuxquestions.org/quest...do=bbcode#code
 
Old 11-11-2016, 07:16 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Member Response

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.

We look forward to seeing your efforts.
 
Old 11-11-2016, 08:54 AM   #6
ryganys
LQ Newbie
 
Registered: Nov 2016
Posts: 9

Original Poster
Rep: Reputation: Disabled
process_file.txt

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
 
Old 11-11-2016, 08:58 AM   #7
ryganys
LQ Newbie
 
Registered: Nov 2016
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01 View Post
Please show us what you've tried so far, using CODE tags https://www.linuxquestions.org/quest...do=bbcode#code
Im actually stuck there, i have taken no further actions..
 
Old 11-11-2016, 09:17 AM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Member Response

Quote:
Originally Posted by ryganys View Post
Im actually stuck there, i have taken no further actions..
I recommend a few first steps here:
  1. 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.
  2. Add the line:
    Code:
    #!/bin/bash
    as the first line of your script.
  3. 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.
 
1 members found this post helpful.
Old 11-11-2016, 09:26 AM   #9
ryganys
LQ Newbie
 
Registered: Nov 2016
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
I recommend a few first steps here:
  1. 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.
  2. Add the line:
    Code:
    #!/bin/bash
    as the first line of your script.
  3. 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.
 
Old 11-11-2016, 09:35 AM   #10
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Member Response

Quote:
Originally Posted by ryganys View Post
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.
 
Old 11-11-2016, 09:59 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
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.
 
1 members found this post helpful.
Old 11-11-2016, 10:13 AM   #12
ryganys
LQ Newbie
 
Registered: Nov 2016
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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.
 
Old 11-11-2016, 11:03 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
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.
 
Old 11-11-2016, 11:24 AM   #14
ryganys
LQ Newbie
 
Registered: Nov 2016
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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.
 
Old 11-11-2016, 11:48 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
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?
 
  


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 07:28 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