LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Newbie CentOS Scripting Question.. (https://www.linuxquestions.org/questions/linux-newbie-8/newbie-centos-scripting-question-4175523489/)

Clark-Kent 10-27-2014 12:16 PM

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!

weibullguy 10-27-2014 01:09 PM

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.

Ihatewindows522 10-27-2014 01:59 PM

1 Attachment(s)
Quote:

Originally Posted by Clark-Kent (Post 5260342)
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. ;)

Clark-Kent 10-27-2014 03:55 PM

Quote:

Originally Posted by Ihatewindows522 (Post 5260399)
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!! :D

Fred Caro 10-27-2014 08:21 PM

Did it work?

Fred.

Clark-Kent 10-28-2014 08:57 AM

Quote:

Originally Posted by Fred Caro (Post 5260550)
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! :)

Ihatewindows522 10-28-2014 12:21 PM

Quote:

Originally Posted by Clark-Kent (Post 5260743)
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. ;)

Clark-Kent 10-28-2014 12:25 PM

Quote:

Originally Posted by Ihatewindows522 (Post 5260846)
Like I said, the debugging is up to you. ;)

Yes you did! :D

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

Great learning process!! :)

chrism01 10-29-2014 02:26 AM

Adding
Code:

set -xv
to the top will show you exactly what its doing


All times are GMT -5. The time now is 03:59 AM.