LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 10-27-2014, 01:16 PM   #1
Clark-Kent
LQ Newbie
 
Registered: Oct 2014
Posts: 4

Rep: Reputation: Disabled
Newbie CentOS Scripting Question..


Hi everyone,

Long time forum lurker..first time user here.

I am a COMPLETE newbie to all things Unix/Linux and I was hoping for a little advice (or appropriate direction) on a scripting problem I have been set.

I was been asked (college lecturer) to write a script which will display the user options that which will allow a user to:

1. create a file
2. delete a file
3. change permissions
4. rename a file
5. copy a file
6. exit

I started by creating the script, and echoing the options to screen..but from there I am completely lost!

I have previously used the Case & If selection structures but only to echo input back to screen, but that is about as far as I have gone.

Any advice would be gratefully appreciated!
 
Old 10-27-2014, 02:09 PM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,812
Blog Entries: 1

Rep: Reputation: 259Reputation: 259Reputation: 259
I'm assuming you're writing a Bash script. This is a good reference for Bash scripting. Look in the section on command substitution since you're just writing a script that wraps shell commands. For example, with the first option you could use the shell command touch to create the file.
 
Old 10-27-2014, 02:59 PM   #3
Ihatewindows522
Member
 
Registered: Oct 2014
Location: Fort Wayne
Distribution: Ubuntu 16.04 LTS
Posts: 614
Blog Entries: 2

Rep: Reputation: 166Reputation: 166
Quote:
Originally Posted by Clark-Kent View Post
Hi everyone,

Long time forum lurker..first time user here.

I am a COMPLETE newbie to all things Unix/Linux and I was hoping for a little advice (or appropriate direction) on a scripting problem I have been set.

I was been asked (college lecturer) to write a script which will display the user options that which will allow a user to:

1. create a file
2. delete a file
3. change permissions
4. rename a file
5. copy a file
6. exit

I started by creating the script, and echoing the options to screen..but from there I am completely lost!

I have previously used the Case & If selection structures but only to echo input back to screen, but that is about as far as I have gone.

Any advice would be gratefully appreciated!
OK, first get the menu looking good. We'll add the functionality in later functions.
Code:
menu () {
echo "$name version $ver"
echo "----------------------------------------------"
echo "1) Specify file"
echo "2) Create file"
echo "3) Delete file"
echo
echo "4) Change file permissions"
echo "5) Rename file"
echo "6) Exit"
echo "----------------------------------------------"
read menunum
}
Then, we'll add the functions of the menu.
Code:
op1 () {
clear
echo "Please enter the path (including desired filename) below"
read newfile
touch $newfile
clear
echo "File has been created."
menu
}

op2 () {
clear
echo "Please enter the path of the file to delete below."
read delfile
clear
echo "Would you like to shred the file or just delete it?"
echo "1) Delete"
echo "2) Shred"
echo "--------------------------------------------------"
read delchoice
if [[ $delchoice = 1 ]] ; then 
	rm $delfile
else
	shred -u $delfile
fi
clear
echo "Delete file complete."
menu
}

op3 () {
clear
echo "Please enter the target file path below"
read targfileperms
clear
echo "Please enter the chmod parameters below"
read chmodparams
clear
chmod $chmodparams $targfileperms
echo "Change of permissions on target file complete."
menu
}

op4 () {
#rename file
clear
echo "Please enter the path of the file to be renamed and/or moved"
read path1
clear
echo "Please enter the path of the target file (name included)"
read destpath
mv $path1 $destpath 
clear
echo "Rename complete."
menu
}
Now we want to tie the menu to the menu functions. I'll just use if statements, you can change them to case if you like.
Code:
if [[ menunum = 1 ]] ; then
op1
elif [[ menunum = 2 ]] ; then
op2
elif [[ menunum = 3 ]] ; then
op3
elif [[ menunum = 4 ]] ; then
op4
else 
echo "Please type a valid number option."
menu
fi
And you put it all together, and you get something like the attached.
NOTE: I didn't debug this, if you want to use it, you debug it. You're the one that will be writing it.
Attached Files
File Type: txt script.sh.txt (1.6 KB, 4 views)
 
1 members found this post helpful.
Old 10-27-2014, 04:55 PM   #4
Clark-Kent
LQ Newbie
 
Registered: Oct 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Ihatewindows522 View Post
OK, first get the menu looking good. We'll add the functionality in later functions.
Code:
menu () {
echo "$name version $ver"
echo "----------------------------------------------"
echo "1) Specify file"
echo "2) Create file"
echo "3) Delete file"
echo
echo "4) Change file permissions"
echo "5) Rename file"
echo "6) Exit"
echo "----------------------------------------------"
read menunum
}
Then, we'll add the functions of the menu.
Code:
op1 () {
clear
echo "Please enter the path (including desired filename) below"
read newfile
touch $newfile
clear
echo "File has been created."
menu
}

op2 () {
clear
echo "Please enter the path of the file to delete below."
read delfile
clear
echo "Would you like to shred the file or just delete it?"
echo "1) Delete"
echo "2) Shred"
echo "--------------------------------------------------"
read delchoice
if [[ $delchoice = 1 ]] ; then 
	rm $delfile
else
	shred -u $delfile
fi
clear
echo "Delete file complete."
menu
}

op3 () {
clear
echo "Please enter the target file path below"
read targfileperms
clear
echo "Please enter the chmod parameters below"
read chmodparams
clear
chmod $chmodparams $targfileperms
echo "Change of permissions on target file complete."
menu
}

op4 () {
#rename file
clear
echo "Please enter the path of the file to be renamed and/or moved"
read path1
clear
echo "Please enter the path of the target file (name included)"
read destpath
mv $path1 $destpath 
clear
echo "Rename complete."
menu
}
Now we want to tie the menu to the menu functions. I'll just use if statements, you can change them to case if you like.
Code:
if [[ menunum = 1 ]] ; then
op1
elif [[ menunum = 2 ]] ; then
op2
elif [[ menunum = 3 ]] ; then
op3
elif [[ menunum = 4 ]] ; then
op4
else 
echo "Please type a valid number option."
menu
fi
And you put it all together, and you get something like the attached.
NOTE: I didn't debug this, if you want to use it, you debug it. You're the one that will be writing it.


Wow, thank you VERY much!! I didn't expect that level of help..but REALLY appreciated!!
 
Old 10-27-2014, 09:21 PM   #5
Fred Caro
Member
 
Registered: May 2007
Posts: 999

Rep: Reputation: 166Reputation: 166
Did it work?

Fred.
 
Old 10-28-2014, 09:57 AM   #6
Clark-Kent
LQ Newbie
 
Registered: Oct 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Fred Caro View Post
Did it work?

Fred.
Not quite..I am getting a "syntax error near unexpected token 'fi' message.

Playing around with it, but not luck fixing it yet..any suggestions if someone sees something I don't would be appreciated!
 
Old 10-28-2014, 01:21 PM   #7
Ihatewindows522
Member
 
Registered: Oct 2014
Location: Fort Wayne
Distribution: Ubuntu 16.04 LTS
Posts: 614
Blog Entries: 2

Rep: Reputation: 166Reputation: 166
Quote:
Originally Posted by Clark-Kent View Post
Not quite..I am getting a "syntax error near unexpected token 'fi' message.

Playing around with it, but not luck fixing it yet..any suggestions if someone sees something I don't would be appreciated!
Like I said, the debugging is up to you.
 
Old 10-28-2014, 01:25 PM   #8
Clark-Kent
LQ Newbie
 
Registered: Oct 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Ihatewindows522 View Post
Like I said, the debugging is up to you.
Yes you did!

Nearly there..sorted out the 'fi' bug, a couple more to finish off..hopefully!!

Great learning process!!
 
Old 10-29-2014, 03:26 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Adding
Code:
set -xv
to the top will show you exactly what its doing
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Newbie bash scripting question xenodamus Linux - Newbie 4 01-31-2014 12:54 PM
Newbie Bash Scripting Question jsaxton86 Programming 2 11-26-2005 08:58 PM
Newbie scripting question, dates Bubba235 Linux - Enterprise 8 11-05-2004 01:06 AM
Newbie bash scripting question... cmfarley19 Linux - Newbie 2 05-03-2003 09:26 AM
A Newbie,s Scripting Question newpenguin Linux - Newbie 7 10-07-2002 01:52 PM


All times are GMT -5. The time now is 12:12 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration