Tag Archives: bash

Shell,(bash) tiny thing but might be important, [: missing `]’

Source from: http://stackoverflow.com/questions/15993062/bash-scripting-missing

I am getting an error ./test.sh: line 13: [: missing `]’ in the file test.sh I tried using brackets and other options such as -a or by checking the size of the file p1 but the error is always there and the else statement is always executed irrespective of the input given.I even tried by removing the ; in line 13 but it didn’t help. test.sh

    #!/bin/bash
    echo "Enter app name"
    read y
    $y &
    top -b -n 1 > topLog.log
    #-w checks for the whole word not and sub string from that word
    grep -w "$y" topLog.log > p1
    #-s option checks if the file p1 is present or not
    if [ -s "p1"];  #line 13
    then 
        echo "Successful "
    else
        echo "Unsuccessful"
    fi
    rm p1

I am new to bash scripting.So if there is any silly mistake please excuse me.

Change

if [ -s "p1"];  #line 13

into

if [ -s "p1" ];  #line 13

note the space.

How to write your own shell script?

I need to write a simple shell script to copy library from A to B.

 

Put this code on top of your shell script file (any text file), and write shell commands such as cp,mv and rm.

<code>#!/bin/bash</code>

 

 

  • This will get you past the details of writing and launching a simple script.
    1. Choose a text editor you want to use. It can be a command-line editor like emacs, pico or vi, or an X Windows editor if you have this option.
    2. Run your choice of editor and type the following lines:
      #!/bin/bash
      echo "Hello, world."

       

      NOTE: Be sure to place a linefeed at the end of your script. Forgetting a terminating linefeed is a common beginner’s error.

    3. Save the file in the current working directory as “myscript.sh”.
    4. Move from the text editor to a command shell.
    5. From the command shell, type this:$ chmod +x myscript.sh
    6. To execute the script, type this:$ ./myscript.sh
      Hello, world.
  • These steps will become second nature soon enough.

More detail information can be found following link.

http://www.arachnoid.com/linux/shell_programming.html