ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Okay I got another error after running this script:
Code:
#!/bin/bash
function f_print_msg {
echo "Please choose a file or directory. If you choose a directory you will move into that folder. Enter nothing to exit."
read -p "$(pwd) => " choice
f_print_msg
while [ "$choice" != "" ] ;
do
## Enter your code here
echo "$choice options:"
echo "Type hide to hide file."
echo "Type copypro to copy protect."
echo "Type read to set ready only."
echo "Type copy to copy a file from one directory to another."
echo "Type exit to choose a different file."
read op
case "$op" in
hide)
mv "$choice" ".$choice";;
copy)
cp "$choice" "~/$choice";;
copypro)
chmod 333 "$choice" ;;
read)
chmod 555 "$choice" ;;
exit)
exit
*)
#code for error
esac
else
cd $choice
fi
fi
f_print_msg
done
}
Error Message:
Code:
root@localhost Project]# ./Project3
./Project3: line 35: syntax error near unexpected token `else'
./Project3: line 35: ` else'
Okay after correcting the syntax, I'm not getting an errors, but I don't if the script that created is what's project is asking for. Can anybody help on that?
Here is Project 3 Description
Quote:
PROJECT 3
Create your own shell that will allow you to change file status and copy from one directory to another.
Code:
#!/bin/bash
function f_print_msg {
echo "Please choose a file or directory. If you choose a directory you will move into that folder. Enter nothing to exit."
read -p "$(pwd) => " choice
f_print_msg
while [ "$choice" != "" ] ;
do
## Enter your code here
echo "$choice options:"
echo "Type hide to hide file."
echo "Type copypro to copy protect."
echo "Type read to set ready only."
echo "Type copy to copy a file from one directory to another."
echo "Type exit to choose a different file."
read op
case "$op" in
hide)
mv "$choice" ".$choice";;
copy)
cp "$choice" "~/$choice";;
copypro)
chmod 333 "$choice" ;;
read)
chmod 555 "$choice" ;;
exit)
exit ;;
*)
#code for error
esac
cd $choice
f_print_msg
done
}
You have this line here:
echo "Please choose a file or directory. If you choose a directory you will move into that folder. Enter nothing to exit."
But you don't have anything after to check if the input is a file, or a directory.
Also your syntax is wrong again. Several people including me already corrected this specific syntax error. I'll give you a hint this time.
Check where the f_print_msg function is ending and where it should end.
#!/bin/bash
function f_print_msg {
echo "Please choose a file or directory. If you choose a directory you will move into that folder. Enter nothing to exit."
read -p "$(pwd) => " choice
f_print_msg
while [ "$choice" != "" ] ;
do
## Enter your code here
echo "$choice options:"
echo "Type hide to hide file."
echo "Type copypro to copy protect."
echo "Type read to set ready only."
echo "Type copy to copy a file from one directory to another."
echo "Type exit to choose a different file."
read op
case "$op" in
hide)
mv "$choice" ".$choice";;
copy)
cp "$choice" "~/$choice";;
copypro)
chmod 333 "$choice" ;;
read)
chmod 555 "$choice" ;;
exit)
exit ;;
*)
#code for error
esac
cd $choice
if [ -d "$file" ]; then
#... do something
fi
f_print_msg
done
Error
Code:
[root@localhost Project]# ./Project3
./Project3: line 39: syntax error near unexpected token `fi'
./Project3: line 39: `fi '
[root@localhost Project]#
I didn't mean literally. "$file" is a variable.
It was meant for you to replace with the right one(in this case $choise).
This pretty much shows that you have very much to learn yet. There's nothing wrong with that, but I suggest a different approach for you besides coming to a forum and expecting someone to resolve your school work.
I suggest you a very good tutorial. http://tldp.org/LDP/abs/html/
You will have to learn at learn what variables are for, and how functions work. Any question behind that you are just asking for people to solve it for you, and I doubt anyone here or anywhere will do the school work for you.
I will give you a hint. Study the "tests", "Introduction to Variables and Parameters" and "Loops and Branches" of the link I gave you.
Good luck.
Hey Mann, thank for the reference, but could you able help with Project. I have submit tomorrow. I already started reading Shell Script in 24, then I'll read on your reference.If you can help with this
We're stuck in a loop here: Everyone is giving OP hints, and OP seems to be saying: "just help me".
deven*; A while back, there were a few suggestions about how to debug. (Mine was to insert echo statements to confirm what the program is doing at various steps.) Did you try this or any other similar suggestions?
I can appreciate your frustration but the cycle of "I tried that and now it does this." is not an efficient way to de-bug your code. The only way to do it is to go line-by-line and do tests to see if everything is OK to that point.
Hey Mann, thank for the reference, but could you able help with Project. I have submit tomorrow. I already started reading Shell Script in 24, then I'll read on your reference.If you can help with this
THanks
Devn
You are not asking: "i really need it so please help me.".
You are asking: "I really need help so please do my homework.".
So it's not a matter of help. You already had plenty of help.
Can anybody help me with this Unix Script? I'm getting the same error message as before.
Yes, many people can but few will until you show that you have tried what has already been suggested. Now you've missed the deadline the pressure is off and you can review this thread and the advice given in it, try a few things and report back. It's not that LQers are sadistic bar stewards but we like to give people fishing rods, not fish. If you follow the suggestions given, you will learn about shell script and then you can help yourself.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.