Announcement

Collapse
No announcement yet.

Bash scripting

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Bash scripting

    Hi all ive created a few scripts id like to call them from another script.

    Here is what i have so far

    Code:
     
    #!/bin/bash
    
    echo "some text"
    echo "some text"
    echo "1. Option 1" 
    
    read -p "Use The Number Keys To Make a Selection : " choice
    
    If [ $choice == 1] ; then
    /home/myhome/scripts/scripta.sh
    
    else
    
    echo "Sorry That Was  Not A Valid Choice" 
    
    fi
    exit
    If you press number 1 the script exits without running the other script any ideas where i messed up?

    Sent from my POT-LX1 using Tapatalk
    Tutorials:
    Yoda's ownCloud Installation on Kubuntu 20.04

    #2
    #1 You captailized "If"
    #2 You need a space before the second bracket "]"

    if [ $choice == 1 ] ; then

    Please Read Me

    Comment


      #3
      Originally posted by oshunluvr View Post
      #1 You captailized "If"
      #2 You need a space before the second bracket "]"

      if [ $choice == 1 ] ; then
      Im still having problems here is my terminal output so you can see what is happening.

      Code:
       
      @server:~$ sudo ./main.sh
      Welsome To my user Tool-Kit 
      
      Please Select an Option
      1. System Administration
      2. Kodi Libary User Accounts
      
      Use The Number Keys To Make a Selection : 2
      ./main.sh: line 13: [/home/myuser/ytk/lu.sh: No such file or directory
      @server:~$
      here is my prototype scipt

      Code:
      #!!/bin/bash                                                                  
      echo "Welsome To username Tool-Kit"
      echo ""
      echo "Please Select an Option"
      echo "1. System Administration"
      echo "2. Kodi Libary User Accounts"
      echo""
      
      read -p "Use The Number Keys To Make a Selection : " choice                  
      if [ $choice == 2 ] ; then [/home/myhome/ytk/lu.sh ];
      else
      
      echo "Sorry That Was  Not A Valid Choice"
      
      fi
      exit
      Last edited by hightokeinyoda; Jul 17, 2020, 04:30 PM. Reason: Fixed code tags
      Tutorials:
      Yoda's ownCloud Installation on Kubuntu 20.04

      Comment


        #4
        Try
        if [ $choice == 2 ] ; then /home/myhome/ytk/lu.sh ;
        [without the square brackets]

        Comment


          #5
          Originally posted by Don B. Cilly View Post
          Try
          if [ $choice == 2 ] ; then /home/myhome/ytk/lu.sh ;
          [without the square brackets]
          Still no joy it gives me syntax errors saying theres an unexpected token near then.

          When you say without the square brackets do you mean remove all of them feom that line?

          Sent from my POT-LX1 using Tapatalk
          Tutorials:
          Yoda's ownCloud Installation on Kubuntu 20.04

          Comment


            #6
            Oh well. Try:
            Code:
            #!!/bin/bash                                                                  
            echo "Welcome To username Tool-Kit"
            echo ""
            echo "Please Select an Option"
            echo "1. System Administration"
            echo "2. Kodi Libary User Accounts"
            echo""
            
            read -p "Use The Number Keys To Make a Selection : " choice                  
            if [ $choice -eq 2 ] ; then /home/myhome/ytk/lu.sh ;
            else
            
            echo "Sorry That Was  Not A Valid Choice"
            
            fi
            exit
            which should work.
            Thing is, I got used to case... :·)
            So I would:

            Code:
            #!!/bin/bash                                                                  
            echo "Welcome To username Tool-Kit"
            echo ""
            echo "Please Select an Option"
            echo "1. System Administration"
            echo "2. Kodi Libary User Accounts"
            echo""
            
            read -p "Use The Number Keys To Make a Selection : " choice                  
            case "$choice" in
            "1")
             echo "Oops." ;
             ;;
            "2")
             /home/myhome/ytk/lu.sh ;
             ;;
            *)
             echo "invalid choice"
             ;;
            esac
            exit
            Which also works

            Comment


              #7
              Part of the problem is you ask for help, then change a bunch of stuff along with what the advice was, then it doesn't work again.

              For example, you now have added a second ! in front of /bin/bash which will cause an interpreter error, you added brackets for some reason around your action which caused yet another set of errors - again no space between a bracket and the object and no reason for those brackets at all, and you added another ; which is causing yet another error.

              Programming languages, even simple ones like bash, have rules and they must be followed. You can't just willie-nillie put things in there and expect them to work.

              I appreciate your desire to have something cool like your own custom tailored script, but you should probably do some web searching for bash examples and follow their lead or even take a short tutorial somewhere. It will help you learn faster and you'll get more personal satisfaction by gaining the knowledge.

              Correcting the simple errors I mentioned above let the script work:
              Code:
              #!/bin/bash
              echo "Welsome To username Tool-Kit"
              echo ""
              echo "Please Select an Option"
              echo "1. System Administration"
              echo "2. Kodi Libary User Accounts"
              echo""
              
              read -p "Use The Number Keys To Make a Selection : " choice
              if [ $choice == 2 ] ; then /home/myhome/ytk/lu.sh 
              else
              
              echo "Sorry That Was  Not A Valid Choice"
              
              fi
              exit
              Last edited by oshunluvr; Jul 18, 2020, 02:17 PM.

              Please Read Me

              Comment


                #8
                One good tip to start is to follow a basic readable pattern using indents, use comments to define sections of the script, and spread out your lines so you can learn where the issues are, because you often get a line reference with an error. For example, this:

                Code:
                #!/bin/bash
                # intro
                echo "Welcome To username Tool-Kit"
                echo 
                echo "Please Select an Option"
                echo "1. System Administration"
                echo "2. Kodi Libary User Accounts"
                echo
                
                # get input
                read -p "Use The Number Keys To Make a Selection : " choice
                
                # do something
                if [ $choice == 2 ] ; 
                  then
                    /home/myhome/ytk/lu.sh 
                  else
                    echo "Sorry That Was  Not A Valid Choice"
                    exit 1
                fi
                #end
                is a little more readable because the indents help define where things belong and the remarks guide you through the script. I realize these may not be important with a script this small but if you develop good habits with your little scripts, you'll find really big ones more manageable in the future.

                Because for sure, once you get a taste for the power of scripting and the control it gives you - you will do more and more of them!

                Please Read Me

                Comment


                  #9
                  It also occurred to me you might want something like this for your first line:

                  echo "Welcome yo your Tool-Kit, "$USER

                  This will put your actual username in there.

                  Please Read Me

                  Comment


                    #10
                    and since I'm now playing around...

                    ... If you're like me and your user name is lower case but you'd like it in capitalized for a script, use this

                    ${USER^}

                    instead of

                    $USER

                    and you'll get a leading capital letter.

                    Please Read Me

                    Comment


                      #11
                      Originally posted by oshunluvr View Post
                      and since I'm now playing around...

                      ... If you're like me and your user name is lower case but you'd like it in capitalized for a script, use this

                      ${USER^}

                      instead of

                      $USER

                      and you'll get a leading capital letter.
                      Sorry ive had a problem with my ic my provider sorted it, now ive got that sorted ill do some web searching for bash tutorials you wouldn't happen to know of a good site would you.

                      I found alot of tutorials but they seem out dated or incorrect and in one or two cases incomplete

                      Sent from my POT-LX1 using Tapatalk
                      Tutorials:
                      Yoda's ownCloud Installation on Kubuntu 20.04

                      Comment

                      Working...
                      X