LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-28-2014, 08:20 AM   #1
Linux hatesme
LQ Newbie
 
Registered: Jul 2014
Posts: 8

Rep: Reputation: Disabled
Need help and guidance bash scripting


Good morning all. I have been working on this for over a week and this is what I have. Please be gentle and explain to me what I have done wrong or right. Also if there is any way to make this code shorter THANK YOU. MY instructions and code follows.
Instructions
project will consist of multiple menu items that have sub menus for multiple functions. They are as follows:

Option 1 File functions

Subfunction Open file and enter five comic names –close file

Subfuntion2 Open file and read names from file above

Subfunction3 – Prompt user for file name and search for that file-display results

Subfunction4 – Prompt user for folder to search for and display results

Option 2 Network functions

Subfunction 1Prompt user for IP address and ping that address

Subfunction2 Display all used ip addresses on your network

Option 3 Math functions

Subfunction 1 prompt user for two numbers and add them together display result

Subfunction 2 prompt user for two numbers and multiply them together display result

Option 4 Display Options

Subfunction 1- Display the following (you must use loops – no separate print line functions)

*

**

***

****

*****

****

***

**

*

Option 5 Adminusers

Subfunction1 – display users

Subfunction 2- add user

Subfunction3- delete user

Subfunction4- change user password

(in all of the user functions you will prompt admin user for name to add, delete, etc)

Option 6 Date

Subfunction1 show current date

Subfunction2 show calendar for current month

Subfunction3 test to show if today is Friday the 13th or not

Option 7- email functions

Subfunction 1-test to see if smtp server is installed

Subfunction 2- send test email to current user

My code
Code:
#!/bin/bash

trap '' 2
while true
do
  clear
  echo "=========="
  echo "Menu"
  echo "=========="
  echo "Enter 1 File functions:  "
  echo "Enter 2 Network functions:  "
  echo "Enter 3 Math functions:  "
  echo "Enter 4 Display Options:  "
  echo "Enter 5 Adminusers:  "
  echo "Enter 6 Date:  "
  echo "Enter 7 Email functions:  "
  echo "Enter 8 to exit menu:  "

  echo "Enter your selection "
  read answer
    case "$answer" in
    1)
File Funtions ()
{
clear
echo "=========="
echo "File Functions"
echo "a) open file and enter five comic names"
echo "b) Open file and read names from file above"
echo "c) Enter File name to search"
echo "d) Enter folder to search"
echo "e) Quit to Main Menu"
echo "=========="
while :
do
read answer
case $answer in
"a")
open blank.txt ;;
"b")
open blank.txt and read ;;
"c") 
 find ./ -type f -name \$1 ;;
"d")
 find . -type d  ;;
"e")
Quit to Main Menu ;;
esac
done
}
File Functions;
;;
Network Functions ()
{
clear
echo "=========="
echo "Network Functions"
echo "a) Enter ip address to Ping"
echo "b) Display all used ip addresses on network"
echo "=========="
while :
do
read answer
case $answer in
"a")
 ping ;;
"b")
 $ /sbin/ifconfig ;;
esac
done
}
Network Functions;
;;
Math Functions ()
{
clear
echo "=========="
echo "Math Functions"
echo "a) Enter 2 numbers to add together"
echo "b) Enter 2 numbers to multiply together"
echo "=========="
while :
do
read answer
case $answer in
"a")
echo -n "Number1: "
read num1
echo -n "Number2: "
read num2
echo "$num1 + $num2 = 'expr $num1 + $num2'" ;;
"b")
echo -n "Number1: "
read num1
echo -n "Number2: "
read num2
echo "$num1 * $num2 = 'echo "$num1*$num2"|bc'" ;;
esac
done
}
Math Functions;
;;
Display Options ()
{
clear
echo "=========="
echo "Display Options"
echo "a) option"
echo "=========="
while :
do
read $answer
case $answer in
"a")
for (( c=*; c<=*****; c>=*; c++ ))
do
echo "$c" ;;
done
}
Display Options;
;;
Adminusers ()
{
clear
echo "=========="
echo "Adminusers"
echo "Enter 1 to add user 1:  "
echo "Enter 2 to delete user 2:  "
echo "Enter 3 to change user password 3:  "
echo "Enter q to exit the menu q:  "

echo "Enter your selection \c"
read answer
case "$answer" in
1) useradd ;;
2) userdel ;;
3) passwd ;;
q) exit ;;
esac
done
}
Adminusers;
;;
Date ()
{
clear
echo "=========="
echo "Date"
echo "a) Current Date"
echo "b) Show Calender"
echo "c) Friday the 13th"
echo "=========="
while :
do
read answer
case $answer in
"a")
do 
date
echo $words
;;
"b")
do 
cal
echo $words 
;;
"c")
DAY=$(date +%a)
if [ $DAY == "Fri" ]
then
echo "Today is Friday"
fi
esac
done
}
Date;
;;
Email Functions ()
{
clear
echo "=========="
echo "Email Functions"
echo "a) Test if local SMTP server is running"
echo "b) Send test email"
echo "=========="
while :
do
read answer
case $answer in
"a")
do 
 # echo -e "quit" | nc localhost 25
220 ubuntu ESMTP Postfix (Ubuntu)
221 2.0.0 Bye
;;
"b")
do 
du -sh | mail -s "test" user@yourmaildomain.com
 ;;
esac
done
}
Email Functions;
;;
Quit ()
{
esac
done
}
Quit;
;;
    3) passwd ;;
    4) exit ;;
esac
done
End of code Thank you for shareing your knowledge with me.

Last edited by Linux hatesme; 07-28-2014 at 09:13 AM. Reason: asked to add code tags
 
Old 07-28-2014, 08:41 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please edit post and place code / data inside [code][/code] tags to make it more readable.
 
Old 07-28-2014, 09:15 AM   #3
Linux hatesme
LQ Newbie
 
Registered: Jul 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thank you for the quick response and I have already learned something new code tags cool.
 
Old 07-28-2014, 10:30 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well I guess my first question back to you would be .... does what you have done work?

As it is a project (I assume for a course) I would strongly suggest you to go back and read your course notes a bit further.

Some things for you to look for:

1. Variable and function naming limitations, ie. what constitutes a valid variable or function name

2. Where best to define functions so as to make the code cleaner and easier to follow

3. Do all of the commands you have called in the code perform correctly at the command line (ie when not being run from your script)

4. What does the 'trap' command do

5. Investigate the 'select' bash command

6. Do you need to check the data being entered by the user

7. Do you have the necessary permissions for all commands / directories / files

These should give you something to work on.
 
Old 07-29-2014, 10:11 AM   #5
Linux hatesme
LQ Newbie
 
Registered: Jul 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Grail thank you for your response, To answer your questions You presume correct this is for a course. No it does not work as I asked in my first post if someone would please point out what is right and what was wrong and if possible explain why. This is an online course there are no course notes the professor takes a week to respond to an email. My book for the course is based on the principle that I already know all the basic fundamentals which I do not. I am only allowed to have the basic version installed with no updates and no G.U.I. only C.L.I.. Hence why I have come to the forums to actually learn from someone who knows Linux bash scripting. As I have now spent 13 days working on this with no clue what is up or down. Coming from a windows background my terminology is wrong when trying to look up examples and explanations for Linux. That is why I chose the name Linux hates me because there is a huge misunderstanding in basic thought between me and Linux. I spent 4 hours looking up Variable and Function limitations and I am still no closer to understanding how it works. I do understand that there are specific commands that need other commands placed in different formats to allow them to play nice with each other. 2. Still no clue. 3. I presume you mean when typing in a text editor. Again I don't really know as I am color blind my friend told me my shabang line was blue then laughed at me. 4. I believe that the trap command is used to stop an endless loop. 5. I have read about the select command and have read more than a few times that it can and will create problems unless you know what you are doing. 6. Yes I think it does need to check the answer by the user to know what to do if that makes sense. 7. I presume you mean sudo or root which I enact at the command line when running it at least I think that's where you apply it. I hope that helps again thank you.
 
Old 07-29-2014, 10:20 AM   #6
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
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/

If you read that from top to bottom, you should understand where you are at and what to do about it. But it doesn't come easy. Understanding something to be able to work with it and create with it, takes dedication and time. Not an immediate answer to one small problem.

If you were learning to build and fabricate cars, would you immediately grab a solenoid and say "if someone tells me what I need to know about this solenoid, I will be able to build a car."? Or would you start at the beginning and learn about frames? Then electrical, batteries, wheels, brakes etc?

My point is,.. if you want to do this as a career, you need to get motivated, stay focused and learn the basics first.

Once you've passed the beginner mark, you can work on some advanced stuff:

http://www.tldp.org/LDP/abs/html/

Then later, when you are ready to study systems administration you can hit up:

http://www.tldp.org/LDP/sag/html/index.html

But take your time, and learn to understand what you are doing. And if you are still new to Linux, and don't understand the basics of it, you can even build your understanding of that: http://www.tldp.org/LDP/intro-linux/html/index.html

Last edited by szboardstretcher; 07-29-2014 at 10:23 AM.
 
Old 07-30-2014, 03:22 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I would agree with the above post. You can't simply write all of what you have and then ask others to work out the issues.

You have been provided some excellent material in previous post to help you, so I would suggest you break down each individual section and get them to work in isolation
prior to placing them together.

One point that you have misunderstood me on was point number '3'. I am not talking about a text editor or what shebang you have used.
My question is does the following command from your script work at the command line:
Code:
$ /sbin/ifconfig
If it does you can move on, if it does not you need to make sure whatever you call in your script must first of all work at the command line
 
Old 07-30-2014, 06:53 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Definitely read the links in post #6 and also this http://rute.2038bug.com/index.html.gz.
The best way to learn any programming language is 1 or 2 cmds at a time.
Writing a whole program in one go and then trying to debug it is doing it the hard way; build it up one piece at a time.

One tip; adding
Code:
set -xv
as the 2nd line of your code will help debug it.
 
  


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
LXer: Bash If statements, Exit Status and Comparison Operators, A beginners guide to bash scripting LXer Syndicated Linux News 0 06-29-2014 07:35 PM
BASH Scripting Help skydiverscott Linux - Newbie 10 02-13-2012 02:31 PM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 02:29 AM
bash scripting.. kurrupt Programming 3 09-21-2005 12:07 AM
Bash scripting Gunslinger_ROL Programming 5 09-28-2004 11:37 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:29 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