Linux Lite Forums

Development => Scripting and Bash => Topic started by: bitsnpcs on February 15, 2019, 05:11:20 AM

Title: [SOLVED] Program error
Post by: bitsnpcs on February 15, 2019, 05:11:20 AM
Hello,

this is the script -

(https://i.ibb.co/x6nvNSK/menu-driven.png)

this is the error message on option 3 only


(https://i.ibb.co/JRQMWmN/menu-driven2.png)


I set the shell script permissions to -

Code: [Select]
chmod 755
As it was what the book specified to use in the first chapter of the shell scripting chapters.

I checked the "18G..." results with error , by changing lines #33 and #36 by adding

Code: [Select]
sudo
the program produces no error, but of course it needs the password to be typed, this confirmed the results of "18G...", as correct. In that the script, as in screenshot above, has the permission denied error but is producing the correct results shown on the next line in the terminal after the error, on the screenshot above.

Options 1, 2, and 0 work without any errors in the program.
The only changes I have made to the script from the book is to beautify the code alignment to my liking.
The error happens before this when run, and still does, so it has not affected the programs functioning.

I would like some help as to how to run this script on option3 without it producing the error.
Title: Re: [SOLVED] Program error
Post by: firenice03 on February 15, 2019, 11:10:32 AM
@bitsnpcs
I was playing with the du command in my terminal and could replicate a permissions error - although not the same file as you...
using 'du -sh /home' or 'du -sh /home/*'

What did yield the results without error was 'du -sh ~'
Code: [Select]
du -sh ~
May be worth a try in your script... ??? ...
- Also I'd be curious to the top 14 lines of your script - looks like a nice piece of coding :)

Hope it helps!!!


*** Sorry ~ shows your home directory only.. Missed the need for "others/all users"  got caught up playing LOL ****


***2nd Edit - Although your error is in your own home, perhaps permissions are pooched on your files? See this link  ***
https://askubuntu.com/questions/863042/permission-denied-running-find-command (https://askubuntu.com/questions/863042/permission-denied-running-find-command)
To make your user owner of the files in your home directory


Good Luck
Title: Re: [SOLVED] Program error
Post by: bitsnpcs on February 15, 2019, 03:00:43 PM
@firenice03 here are the top 14 lines and onwards a bit


(https://i.ibb.co/fYwGbjF/menudriven3.png)

Thank You I will give these a try :)
Just looking at edit 2


Title: Re: [SOLVED] Program error
Post by: bitsnpcs on February 15, 2019, 03:15:08 PM
@firenice03 2nd Edit has solved it , thank you  8)

Option 0 screenshot

(https://i.ibb.co/rF5MwB1/Screenshot-2019-02-15-20-24-42.png)

Option 1, 2, 3 , and a screenshot showing the error message for a number entered outside of the 0-3 options.

(https://i.ibb.co/CbRw0JB/menudriven5.png)
Title: Re: [SOLVED] Program error
Post by: firenice03 on February 15, 2019, 06:10:39 PM
@bitsnpcs cool glad it helped, yeah I might mimic that script of yours :)
Title: Re: [SOLVED] Program error
Post by: bitsnpcs on February 15, 2019, 06:28:35 PM
@firenice03 that would be  8) :)

This is the Gedit template I use - https://gist.github.com/LeoIannacone/71028cc3bce04567d77e

Title: Re: [SOLVED] Program error
Post by: firenice03 on February 18, 2019, 08:10:03 PM
@bitsnpcs

So I tweaked it a little, it will run in a loop until 0 = quit or an invalid is chosen.
So you can run each without restarting the script.

Thanks for posting it gave me something to play with LOL

I was wondering what you used to edit - I used Geany on my LL box, I use Atom at work for mostly Ruby (still a newbie to it)..
If you're curious - They both allow you have switch between files in tabs - Atom will show your directory tree, where I have files that reference other files I can switch around and still know where I'm at :)

Here's the modifications.. Minor tweaks (I added some spacing for visual)... All other lines are the same (moved 0 to the bottom so I could think it thru :) )

  (http://imgur.com/3B9G9zBl.png)
 (http://i.imgur.com/3B9G9zB.png)
Title: Re: [SOLVED] Program error
Post by: bitsnpcs on February 19, 2019, 03:53:34 AM
@firenice03

 :) Good modifications.

I haven't learned these yet, loops are in the next chapter, I learn from the book etc.

Yes I use Gedit on Linux Lite for learning Python, and also learning Shell Scripting as it is the final part of the command line book (Chapters #24 thru #36[end of the book]), I have just completed chapter #28, I've been on this book for a long time maybe 3 or 4 years lol
 I have just added the template to Gedit in Ubuntu on Saturday, I couldn't add the template in Gnome myself, one of the Ubuntu Moderators showed me how to do it. I also had to ask how to add the template the first time on Linux Lite.

On Linux Lite I have also used Leafpad for editing code of files already on the system, or files in Virtualbox when I used to use it, just a couple of times, I also use Leafpad for lists some times, in the same way I used to use textpad in Windows in the past.

I just try and learn code at home.
At the weekend I heard about womens mentoring programmes, so I began to look if they exist for Linux, I have to learn more about them and see if there are any suitable for me, or whether I need to learn a lot more first before I would be able to join it, as it may begin at too advanced a level for me to be able to learn on it.
Title: Re: [SOLVED] Program error
Post by: firenice03 on February 19, 2019, 11:42:51 AM
@bitsnpcs
Take your time... You're doing great :)
I wish I had the time to fully learn it, more so trial by fire and learn as you go..
I know enough to get into trouble, and hopefully enough to get back out... But can poke around to get what I need...


 8) 8) 8)
Title: Re: [SOLVED] Program error
Post by: bitsnpcs on February 19, 2019, 03:51:34 PM
@firenice03
I think you know Linux very well :)
Title: Re: [SOLVED] Program error
Post by: bitsnpcs on February 19, 2019, 05:17:19 PM
I had wrote previous the last chapter was #38 , the last Chapter is #36 , I was giving myself more chapters coz I need the practice lol (edited above).

Now I have begun the Chapter 29, I found some more information about it, it says it teaches "looping with while and until"  in Chapter 29, and looking forward for the title, it says "Looping with For" for Chapter 23.
Also the command line found a typo in the book now, on the page 358, line 7 of the example of what a while loop is, it has printed -1e, it should be -le 
I think it is the very first typo I have noticed in this book.
Title: Re: [SOLVED] Program error
Post by: bitsnpcs on February 19, 2019, 06:07:31 PM
This is the while loop version1 [FIXED] from the book, there is to be a version2 of this one later in the chapter 29 using 'continue' as well. I am tired for code now today, I can see version2 also has the same indentation errors and need the same fix as this version, haven't looked at the rest of it yet.

This one you select a menu option it displays it for 3 seconds,  and then it loops back to program to enter another option.I like it I think it could be made in to some nice programs.  The delay can be adjusted on Line 5 to more seconds.

Maybe there can be a $DELAY and a new line of $DELAY2 where 2 has a longer duration and then edit the sleep delay on line 28 to $DELAY2 , it might work, to have two different durations allowing for the more text to read on that option,  I'll try it tomorrow, as I am tired for code now.

(https://i.ibb.co/fX0D2b5/while1.png) (https://ibb.co/Fh7HK9M)


(https://i.ibb.co/bXCHgwV/while2.png) (https://ibb.co/gJfvMq8)


There are 2 errors in the while loop code in the book, I fixed these.Both are indentation errors, these are on lines #9 and #17 they should not be indented like they are in the book, just type it like the screenshots then the program runs correctly.
Title: Re: [SOLVED] Program error
Post by: bitsnpcs on February 19, 2019, 08:55:28 PM
I added a DELAY2 as I mentioned above and it works correctly, so it is 3 seconds on the options 1 and 3 , and 8 seconds on option 2.
Lines changes #6 and #29, only small but it is the first thought and change in shell script I've made other than for alignments.
Added comment line #6, edited comment line #5
Screenshot Lines #31 onwards as above no changes.

(https://i.ibb.co/zJqPrWv/while4.png) (https://ibb.co/gVkt3bK)
Title: Re: [SOLVED] Program error
Post by: firenice03 on February 19, 2019, 09:02:20 PM
Cool - Might have to look at that one too..


Quote
Maybe there can be a $DELAY and a new line of $DELAY2 where 2 has a longer duration and then edit the sleep delay on line 28 to $DELAY2 , it might work, to have two different durations allowing for the more text to read on that option,  I'll try it tomorrow, as I am tired for code now.


You should be able to as long as the variable is expressed... example
DELAY1=2
DELAY2=5
Then call out each specifically within the code..  ***Looks like you tried and it worked!!


You could also use sleep, which when scripting directly from the command line works well..
Example - but substitute in any commands, I may use a variant to watch interfaces or connected HBA cards where I would repeatedly re-run a command.
Code: [Select]
while true
do date
echo " "
sleep 3
done

This will display the Date and a blank line every 3 seconds

Keep it up!!!  ;D ;D
Title: Re: [SOLVED] Program error
Post by: bitsnpcs on February 19, 2019, 09:16:57 PM
Great info thank you  :)
Here is the chapter 29 version2 from book while loop working with break and continue.
Also here I added in the DELAY2, and also fixed the books indentation errors.
It is all on this in the book until chapter 33.

(https://i.ibb.co/2514QVK/w2a.png)

(https://i.ibb.co/qJ5t5J0/w2b.png)