LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Unix Script Help Need (https://www.linuxquestions.org/questions/programming-9/unix-script-help-need-765857/)

deven1174 11-02-2009 07:03 PM

Can someone help me with the modifying this Unix Scripting?

echo "Please choose a folder or file. If you choose a folder you will move into that folder. Enter nothing to exit."

read choice

if [ "$choice" != "" ]; then

if [ -f $choice ]; then

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)

copypro)
chmod 333 "$choice"
read)
chmod 555 "$choice"
exit)
exit
*)
# code for error
esac
else
cd $choice
fi

fi

pixellany 11-02-2009 09:45 PM

At first glance, it looks like you ignored a lot of the questions and comments and simply re-posted your script.

Regardless---looking at the code above: What is the question? Have you run it? What does it do?

Disillusionist 11-03-2009 02:34 AM

Try reading the following:

http://tldp.org/LDP/abs/html/testbranch.html#EX29

This may highlight where you are going wrong with the syntax of your case statement.

deven1174 11-03-2009 02:05 PM

Hello,
I know that I have little time to submit this project. The references that I'm referring to is Sam Teach yourself Shell Programming in 24 hours and this link

http://steve-parker.org/sh/escape.shtml.

I guess I have read this material, and hope to do this project myself, but thanks for help on this. If you anybody can provide more help on this, it would great.

Thanks
Deven

Disillusionist 11-03-2009 02:15 PM

You must seperate arguments to mv with a space character.

Each section of your case statement must be completed by ;;

deven1174 11-03-2009 02:33 PM

Hello,
Based upon what the project is asking for Create your own shell that will allow you to change file status and copy from one directory to another. The above script that provide does that provide the solution to Project 3. Or the script is more in depth, and what professor provided.

Thanks
Deven

Disillusionist 11-03-2009 03:01 PM

There is no way that you could be expected to create your own shell, we can only assume that you are being asked to create a script that asks for file/directory names and process based on this.

Code:

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



f_print_msg()
done


deven1174 11-03-2009 07:06 PM

Can anybody look over this error, I check that line in the script. Can someone tell me what command that goes there?

Thanks

Code:

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

}

Code:

Deven@localhost Project]$ ./Project3
./Project3: line 1: f_print_msg{: command not found
Please choose a file or directory. If you choose a directory you will move into that folder. Enter nothing to exit.
/home/Deven/Desktop/Project => test
./Project3: line 30: syntax error near unexpected token `)'
./Project3: line 30: `                      *)'
[Deven@localhost Project]$


Disillusionist 11-04-2009 12:42 AM

Sorry, I posted without testing.

It's been a while since I wrote a function in shell.

Should have been:
Code:

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



f_print_msg
done


deven1174 11-04-2009 08:45 AM

Hello,
I'm getting same error message as before, can someone can help with project?

Thanks
Deven

pixellany 11-04-2009 08:52 AM

I'm not following the details here......2 general suggestions:

1. Test your code in small pieces---for example, does it correcty run the function "f_print_message".

2. Put echo statements at key locations so you can see where the script is when it fails.

ghostdog74 11-04-2009 08:53 AM

put set -x into your script and run again. This will produce diagnostics and you can see where it goes wrong. put in some effort on your part. this is your project after all.

deven1174 11-04-2009 09:06 AM

Since I'm at work, and there no Linux machine for me check right now. I have wait until, I get home.

deven1174 11-04-2009 05:47 PM

After I ran this script I got this error message with the modifying the code

Code:

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

}

Code:

root@localhost Project]# ./Project3
./Project3: line 1: f_print_msg{: command not found
Please choose a file or directory. If you choose a directory you will move into that folder. Enter nothing to exit.
/home/Deven/Desktop/Project => test
./Project3: line 41: syntax error near unexpected token `done'
./Project3: line 41: `done'
[root@localhost Project]#


ammorais 11-04-2009 05:58 PM

I've just corrected the syntax. The rest of the program is for you to check.


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
# ...
# ...
        f_print_msg
done



All times are GMT -5. The time now is 06:10 AM.