Bash menu

  • mo_mughrabi
  • Student
  • Student
  • Avatar de l’utilisateur
  • Inscription: Nov 07, 2006
  • Messages: 73
  • Status: Offline

Message Mai 19th, 2007, 6:52 am

Bonjour,

Je travaille sur un menu shell en utilisant Solaris (Unix) et quand je me suis déplacé le code pour linux redhat son ne fonctionne pas correctement. Je ne suis pas sûr de ce que doit je mettre à jour dans le code,, s'il vous plaît aider

Code: [ Select ]

#!/bin/sh


#########################
# FILE MENU       #
#########################

fitxt() {

    clear
    echo `date`
    echo
    echo " Please make select"
    echo
    echo "        1. Display the contents of a file"
    echo "        2. Remove a file"
    echo "        3. Copy a file"
    echo "        4. List a file"
    echo "        5. Size of a file"
    echo "        6. Return to main menu"
    echo
    echo "        ENTER YOUR OPTION:"

    

}


filoop() {

    while true
    do
    fitxt
    read answer
    case $answer in
    1)
        DisplayFileContent
    ;;
    2)
        RemvFil    
    ;;
    3)
        CopFil
    ;;
    4)
        ListFil
    ;;
    5)
        SizFill
    ;;
    6)
        helploop
    ;;
    *)
        filoop
    ;;
    esac    
    done

}


DisplayFileContent() {

    clear
    echo "Please enter the full path to file and file name"
    echo
    read filename
    clear
    if [ -f $filename ]
    then
        cat $filename|more
    else
        echo "WRONG FILE NAME"
    fi    
    echo     
    echo "PRESS ENTER TO GO BACK...."
    read GOBACK
    loopfil
}


RemvFil() {

    clear
    echo "Please enter the full path to file you want to delete"
    echo
    read filename
    clear
    if [ -f $filename ]
    then
            echo "Are you sure you want to delete $filename? Type (yes/no)"
        read answer
        if [ $answer = "yes" ]
        then
            rm -f $filename
            echo "FILE WAS REMOVED SUCCESSFULLY"
        fi
    else
        echo "WRONG PARAMETER: Only allowed to enter file name including full path"
    fi
    echo
    echo "PRESS ENTER TO GO BACK...."
    read GOBACK
    loopfil



}


CopFil() {

    clear
    echo "Please enter full path name which you wish to copy? "
    echo
    read filename
    if [ -f $filename ]
    then
        echo "Please enter directory path where you wish to copy the file?"
        read copyto
        if [ -d $copyto ]
        then
            cp $filename $copyto
            echo "FILE WAS COPIED SUCCESSFULLY"
        else
            echo "WRONG PARAMETER: Only directory path are allowed"
        fi
    else
        echo "WRONG PARAMETER: Only allowed to enter file name including full path"
    fi
    echo
    echo "PRESS ENTER TO GO BACK...."
    read GOBACK
    loopfil
}


ListFil() {

    clear
    echo "Please enter path to directory to list down files"
    echo
    read dirname
    clear
    if [ -d $dirname ]
    then
        ls -ltr $dirname|more
    else
        echo "WRONG PATH"
    fi
    echo
    echo "PRESS ENTER TO GO BACK...."
    read GOBACK
    loopfil


}


SizFill() {

    clear
    echo "Please enter file name to get file size"
    echo
    read filname
    clear
    if [ -f $filname ]
    then
        echo "$filname = `ls -ltr $filname| awk {'print '}`"
    else
        echo "WRONG PARAMETER: Only allowed to enter file name including full path"
    fi
    echo
    echo "PRESS ENTER TO GO BACK...."
    read GOBACK
    loopfil

}


##############################
#     End FILE MENU    #
##############################


#################################
#    Text processing menu     #
#################################

proctxt() {
    clear
    echo `date`
    echo
    echo "        Please make your selection"
    echo
    echo "        1. Search a file for a pattern"
    echo "        2. Count lines, words, and characters in specified files"
    echo "        3. Display line differences between two files"
    echo "        4. Return to Main Menu"
    echo
    echo "    Please enter:"
}

procloop() {

    while true
    do
    proctxt
    read answer
    case $answer in
    1)
        SearchFile
    ;;
    2)
        CountLineWordsChars
    ;;
    3)
        DisLineDiff
    ;;
    4)
        helploop
    ;;
     *)
        procloop
    ;;
    esac
    done

}

SearchFile() {

    clear
    echo "Please enter file name you wish to search"
    echo
    read filname
    echo
    
    if [ -f $filname ]
    then
        echo "Please enter key word to search for?"
        read keyword
        egrep $keyword $filname|more
    else
        echo "WRONG PARAMETER: Only allowed to enter file name including full path"
    fi
    echo
    echo "PRESS ENTER TO GO BACK...."
    read GOBACK
    loopfil

}


CountLineWordsChars() {

    clear
    echo "Please enter file name you wish to count words, char and lines"
    echo
    read filname
    echo
    if [ -f $filname ]
    then
        echo "Number of characters in $filname: `cat $filname|wc -m`"
        echo
        echo "Number of lines in $filname: `cat $filname|wc -l`"
        echo
        echo "Number of words in $filename: `cat $filname|wc -w`"
        echo

    else
        echo "WRONG PARAMETER: Only allowed to enter file name including full path"
    fi
    echo
    echo "PRESS ENTER TO GO BACK...."
    read GOBACK
    loopfil
}


DisLineDiff() {

    clear
    echo "Please enter first file name u wish to compare"
    echo
    read filname
    echo
    if [ -f $filname ]
    then
        echo "Please enter second file you want to compare?"
        read filname2
        if [ -f $filname2 ]
        then
            diff $filname $filname2
        else
             echo "WRONG PARAMETER: Only allowed to enter file name including full path"
        fi
        
    else
        echo "WRONG PARAMETER: Only allowed to enter file name including full path"
    fi
    echo
    echo "PRESS ENTER TO GO BACK...."
    read GOBACK
    loopfil

}


#################################
#   End Text processing menu #
#################################

#################################
#      status menu          #
#################################

statxt() {
    clear
    echo `date`
    echo
    echo
    echo " Please make your selection"
    echo
    echo "     1. Display the current date and time"
    echo "     2. Current disk usage"
    echo "     3. Display process status information"
        echo
    echo "     4. Return to main menu"
    echo
    echo "     ENTER YOUR OPTION:"
}


statloop() {

  while true
    do
    statxt
    read answer
    case $answer in
    1)
        DisDateTime
    ;;
    2)
        DiskSpace
    ;;
    3)
        DisplayProcStatus
    ;;
    4)
        helploop
    ;;
    *)
        statloop
    ;;
    esac
    done
}


DisDateTime() {

    clear
    echo
    echo "        DATE:     `date '+%b %d %Y'`"
    echo
    echo "        TIME:         `date '+%H:%M '`"
    echo
    echo "PRES ANY KEY TO CONTINUE......"
    echo
    read answer
    statloop
}



DiskSpace() {

    clear
    echo
    echo "        DISK SPACE..."
    echo
    df -k
    echo
    echo
    echo "PRES ANY KEY TO RETURN BACK...."
    read answer
    loopstat


}

DisplayProcStatus() {

    clear
    echo
    echo "        List of running processes...."
    echo
    ps -ef|more
    echo
    read answer
    loopstat
}



########################
#   HELP MENU    #
########################

helptxt() {

    clear
    echo `date`
    echo
    echo
    echo "    Please make select"
    echo
    echo "        1. Help (Main Menu)"
    echo "        2. File Management Commands (Sub-Menu)"
    echo "        3. Text Processing Commands (Sub-Menu)"
    echo "        4. System Status Commands (Sub-Menu)"
    echo
    echo "        5. Exit"
    echo
    echo "        ENTER YOUR OPTION:"
}
helploop() {

    while true
    do
    helptxt
    read answer
    case $answer in
    1)
        helploop
    ;;    
    2)
        filoop
    ;;
    3)
        procloop
    ;;
    4)
        statloop    
    ;;
    5)
        exit
    ;;
    *)
        helploop
    ;;
    esac
    done
}





case "" in
  help)
  helploop
    ;;

  file)
      filoop
  ;;
  
  text)
  procloop
  ;;

  status)
    statloop
    ;;      

  *)
  helploop
    ;;
esac
  1. #!/bin/sh
  2. #########################
  3. # FILE MENU       #
  4. #########################
  5. fitxt() {
  6.     clear
  7.     echo `date`
  8.     echo
  9.     echo " Please make select"
  10.     echo
  11.     echo "        1. Display the contents of a file"
  12.     echo "        2. Remove a file"
  13.     echo "        3. Copy a file"
  14.     echo "        4. List a file"
  15.     echo "        5. Size of a file"
  16.     echo "        6. Return to main menu"
  17.     echo
  18.     echo "        ENTER YOUR OPTION:"
  19.     
  20. }
  21. filoop() {
  22.     while true
  23.     do
  24.     fitxt
  25.     read answer
  26.     case $answer in
  27.     1)
  28.         DisplayFileContent
  29.     ;;
  30.     2)
  31.         RemvFil    
  32.     ;;
  33.     3)
  34.         CopFil
  35.     ;;
  36.     4)
  37.         ListFil
  38.     ;;
  39.     5)
  40.         SizFill
  41.     ;;
  42.     6)
  43.         helploop
  44.     ;;
  45.     *)
  46.         filoop
  47.     ;;
  48.     esac    
  49.     done
  50. }
  51. DisplayFileContent() {
  52.     clear
  53.     echo "Please enter the full path to file and file name"
  54.     echo
  55.     read filename
  56.     clear
  57.     if [ -f $filename ]
  58.     then
  59.         cat $filename|more
  60.     else
  61.         echo "WRONG FILE NAME"
  62.     fi    
  63.     echo     
  64.     echo "PRESS ENTER TO GO BACK...."
  65.     read GOBACK
  66.     loopfil
  67. }
  68. RemvFil() {
  69.     clear
  70.     echo "Please enter the full path to file you want to delete"
  71.     echo
  72.     read filename
  73.     clear
  74.     if [ -f $filename ]
  75.     then
  76.             echo "Are you sure you want to delete $filename? Type (yes/no)"
  77.         read answer
  78.         if [ $answer = "yes" ]
  79.         then
  80.             rm -f $filename
  81.             echo "FILE WAS REMOVED SUCCESSFULLY"
  82.         fi
  83.     else
  84.         echo "WRONG PARAMETER: Only allowed to enter file name including full path"
  85.     fi
  86.     echo
  87.     echo "PRESS ENTER TO GO BACK...."
  88.     read GOBACK
  89.     loopfil
  90. }
  91. CopFil() {
  92.     clear
  93.     echo "Please enter full path name which you wish to copy? "
  94.     echo
  95.     read filename
  96.     if [ -f $filename ]
  97.     then
  98.         echo "Please enter directory path where you wish to copy the file?"
  99.         read copyto
  100.         if [ -d $copyto ]
  101.         then
  102.             cp $filename $copyto
  103.             echo "FILE WAS COPIED SUCCESSFULLY"
  104.         else
  105.             echo "WRONG PARAMETER: Only directory path are allowed"
  106.         fi
  107.     else
  108.         echo "WRONG PARAMETER: Only allowed to enter file name including full path"
  109.     fi
  110.     echo
  111.     echo "PRESS ENTER TO GO BACK...."
  112.     read GOBACK
  113.     loopfil
  114. }
  115. ListFil() {
  116.     clear
  117.     echo "Please enter path to directory to list down files"
  118.     echo
  119.     read dirname
  120.     clear
  121.     if [ -d $dirname ]
  122.     then
  123.         ls -ltr $dirname|more
  124.     else
  125.         echo "WRONG PATH"
  126.     fi
  127.     echo
  128.     echo "PRESS ENTER TO GO BACK...."
  129.     read GOBACK
  130.     loopfil
  131. }
  132. SizFill() {
  133.     clear
  134.     echo "Please enter file name to get file size"
  135.     echo
  136.     read filname
  137.     clear
  138.     if [ -f $filname ]
  139.     then
  140.         echo "$filname = `ls -ltr $filname| awk {'print '}`"
  141.     else
  142.         echo "WRONG PARAMETER: Only allowed to enter file name including full path"
  143.     fi
  144.     echo
  145.     echo "PRESS ENTER TO GO BACK...."
  146.     read GOBACK
  147.     loopfil
  148. }
  149. ##############################
  150. #     End FILE MENU    #
  151. ##############################
  152. #################################
  153. #    Text processing menu     #
  154. #################################
  155. proctxt() {
  156.     clear
  157.     echo `date`
  158.     echo
  159.     echo "        Please make your selection"
  160.     echo
  161.     echo "        1. Search a file for a pattern"
  162.     echo "        2. Count lines, words, and characters in specified files"
  163.     echo "        3. Display line differences between two files"
  164.     echo "        4. Return to Main Menu"
  165.     echo
  166.     echo "    Please enter:"
  167. }
  168. procloop() {
  169.     while true
  170.     do
  171.     proctxt
  172.     read answer
  173.     case $answer in
  174.     1)
  175.         SearchFile
  176.     ;;
  177.     2)
  178.         CountLineWordsChars
  179.     ;;
  180.     3)
  181.         DisLineDiff
  182.     ;;
  183.     4)
  184.         helploop
  185.     ;;
  186.      *)
  187.         procloop
  188.     ;;
  189.     esac
  190.     done
  191. }
  192. SearchFile() {
  193.     clear
  194.     echo "Please enter file name you wish to search"
  195.     echo
  196.     read filname
  197.     echo
  198.     
  199.     if [ -f $filname ]
  200.     then
  201.         echo "Please enter key word to search for?"
  202.         read keyword
  203.         egrep $keyword $filname|more
  204.     else
  205.         echo "WRONG PARAMETER: Only allowed to enter file name including full path"
  206.     fi
  207.     echo
  208.     echo "PRESS ENTER TO GO BACK...."
  209.     read GOBACK
  210.     loopfil
  211. }
  212. CountLineWordsChars() {
  213.     clear
  214.     echo "Please enter file name you wish to count words, char and lines"
  215.     echo
  216.     read filname
  217.     echo
  218.     if [ -f $filname ]
  219.     then
  220.         echo "Number of characters in $filname: `cat $filname|wc -m`"
  221.         echo
  222.         echo "Number of lines in $filname: `cat $filname|wc -l`"
  223.         echo
  224.         echo "Number of words in $filename: `cat $filname|wc -w`"
  225.         echo
  226.     else
  227.         echo "WRONG PARAMETER: Only allowed to enter file name including full path"
  228.     fi
  229.     echo
  230.     echo "PRESS ENTER TO GO BACK...."
  231.     read GOBACK
  232.     loopfil
  233. }
  234. DisLineDiff() {
  235.     clear
  236.     echo "Please enter first file name u wish to compare"
  237.     echo
  238.     read filname
  239.     echo
  240.     if [ -f $filname ]
  241.     then
  242.         echo "Please enter second file you want to compare?"
  243.         read filname2
  244.         if [ -f $filname2 ]
  245.         then
  246.             diff $filname $filname2
  247.         else
  248.              echo "WRONG PARAMETER: Only allowed to enter file name including full path"
  249.         fi
  250.         
  251.     else
  252.         echo "WRONG PARAMETER: Only allowed to enter file name including full path"
  253.     fi
  254.     echo
  255.     echo "PRESS ENTER TO GO BACK...."
  256.     read GOBACK
  257.     loopfil
  258. }
  259. #################################
  260. #   End Text processing menu #
  261. #################################
  262. #################################
  263. #      status menu          #
  264. #################################
  265. statxt() {
  266.     clear
  267.     echo `date`
  268.     echo
  269.     echo
  270.     echo " Please make your selection"
  271.     echo
  272.     echo "     1. Display the current date and time"
  273.     echo "     2. Current disk usage"
  274.     echo "     3. Display process status information"
  275.         echo
  276.     echo "     4. Return to main menu"
  277.     echo
  278.     echo "     ENTER YOUR OPTION:"
  279. }
  280. statloop() {
  281.   while true
  282.     do
  283.     statxt
  284.     read answer
  285.     case $answer in
  286.     1)
  287.         DisDateTime
  288.     ;;
  289.     2)
  290.         DiskSpace
  291.     ;;
  292.     3)
  293.         DisplayProcStatus
  294.     ;;
  295.     4)
  296.         helploop
  297.     ;;
  298.     *)
  299.         statloop
  300.     ;;
  301.     esac
  302.     done
  303. }
  304. DisDateTime() {
  305.     clear
  306.     echo
  307.     echo "        DATE:     `date '+%b %d %Y'`"
  308.     echo
  309.     echo "        TIME:         `date '+%H:%M '`"
  310.     echo
  311.     echo "PRES ANY KEY TO CONTINUE......"
  312.     echo
  313.     read answer
  314.     statloop
  315. }
  316. DiskSpace() {
  317.     clear
  318.     echo
  319.     echo "        DISK SPACE..."
  320.     echo
  321.     df -k
  322.     echo
  323.     echo
  324.     echo "PRES ANY KEY TO RETURN BACK...."
  325.     read answer
  326.     loopstat
  327. }
  328. DisplayProcStatus() {
  329.     clear
  330.     echo
  331.     echo "        List of running processes...."
  332.     echo
  333.     ps -ef|more
  334.     echo
  335.     read answer
  336.     loopstat
  337. }
  338. ########################
  339. #   HELP MENU    #
  340. ########################
  341. helptxt() {
  342.     clear
  343.     echo `date`
  344.     echo
  345.     echo
  346.     echo "    Please make select"
  347.     echo
  348.     echo "        1. Help (Main Menu)"
  349.     echo "        2. File Management Commands (Sub-Menu)"
  350.     echo "        3. Text Processing Commands (Sub-Menu)"
  351.     echo "        4. System Status Commands (Sub-Menu)"
  352.     echo
  353.     echo "        5. Exit"
  354.     echo
  355.     echo "        ENTER YOUR OPTION:"
  356. }
  357. helploop() {
  358.     while true
  359.     do
  360.     helptxt
  361.     read answer
  362.     case $answer in
  363.     1)
  364.         helploop
  365.     ;;    
  366.     2)
  367.         filoop
  368.     ;;
  369.     3)
  370.         procloop
  371.     ;;
  372.     4)
  373.         statloop    
  374.     ;;
  375.     5)
  376.         exit
  377.     ;;
  378.     *)
  379.         helploop
  380.     ;;
  381.     esac
  382.     done
  383. }
  384. case "" in
  385.   help)
  386.   helploop
  387.     ;;
  388.   file)
  389.       filoop
  390.   ;;
  391.   
  392.   text)
  393.   procloop
  394.   ;;
  395.   status)
  396.     statloop
  397.     ;;      
  398.   *)
  399.   helploop
  400.     ;;
  401. esac
  • Anonymous
  • Bot
  • No Avatar
  • Inscription: 25 Feb 2008
  • Messages: ?
  • Loc: Ozzuland
  • Status: Online

Message Mai 19th, 2007, 6:52 am

Afficher de l'information

  • Total des messages de ce sujet: 1 message
  • Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 68 invités
  • Vous ne pouvez pas poster de nouveaux sujets
  • Vous ne pouvez pas répondre aux sujets
  • Vous ne pouvez pas éditer vos messages
  • Vous ne pouvez pas supprimer vos messages
  • Vous ne pouvez pas joindre des fichiers
 
 

© 2011 Unmelted, LLC. Ozzu® est une marque déposée de Unmelted, LLC