Results 1 to 8 of 8

Thread: shell script

  1. #1
    Join Date
    May 2013
    Beans
    48

    Cool shell script

    Hi Friends,
    This is my first unix programme, but I am facing some syntax error. Please tell me where am I making mistake in this code.
    Following is the same code which I am trying to run:

    echo "This is my first shell programe"
    a=1
    c=`ls /media/ | wc -l`
    while [ $a -eq 1 ]
    do
    echo " System is checking the /media/ folder"
    b= `ls /media/ | wc -l`
    if [ $c -ne $b ] then
    echo "a new device found"
    $a= ` expr $a + 1 `
    fi
    done
    if [ $c -eq $b ] then
    echo "no new device found"
    fi


    I am getting the following syntax error:

    ./pendrive.sh: line 11: syntax error near unexpected token `fi'
    ./pendrive.sh: line 11: `fi'



  2. #2
    Join Date
    Aug 2011
    Location
    52° N 6° E
    Beans
    3,178
    Distro
    Xubuntu 16.04 Xenial Xerus

    Re: Which Ubuntu will be best for Sony Vaio E Series VPCEG28FN?

    If you put "then" on the same line as "if" you have to put a ; inbetween. Else move "then" to the next line.
    Code:
    if [test]; then
    #or
    if [test]
    then
    Besides, the last if statement will never be true. Have you found some good guide on the basics of shell scripting?
    Last edited by Impavidus; May 22nd, 2013 at 12:18 PM.

  3. #3
    Join Date
    Oct 2009
    Location
    Reykjavík, Ísland
    Beans
    11,601
    Distro
    Lubuntu 16.04 Xenial Xerus

    Re: shell script

    Please keep only one topic in each thread.
    I have moved your posts to Programming Talk.
    Bringing old hardware back to life. About problems due to upgrading.
    Please visit Quick Links -> Unanswered Posts
    Don't use this space for a list of your hardware. It only creates false hits in the search engines.

  4. #4
    Join Date
    May 2013
    Beans
    48

    Re: shell script

    echo "This is my first shell programe"
    a=1
    c=`ls /media/ | wc -l`
    while [ $a -eq 1 ]
    do
    echo " System is checking the /media/ folder"
    b= `ls /media/ | wc -l`
    if [ $c -ne $b ]; then
    echo "a new device found"
    $a= ` expr $a + 1 `
    fi
    done
    if [ $c -eq $b ]; then
    echo "no new device found"
    fi

    error:
    ./pendrive.sh: line 7: 1: command not found
    ./pendrive.sh: line 8: [: 1: unary operator expected

    Still now the above problem occurs.

  5. #5
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: shell script

    Quote Originally Posted by Rahul04789 View Post
    error:
    ./pendrive.sh: line 7: 1: command not found
    Remove the space after =
    Code:
    b=_`ls /media/ | wc -l`

  6. #6
    prodigy_ is offline May the Ubuntu Be With You!
    Join Date
    Mar 2008
    Beans
    1,219

    Re: shell script

    Is that just for practice or are you going to use the script? For the latter you should consider making some changes:
    1) If you're actually looking for devices, you should monitor the contents of /dev folder (or, even better, use something like sudo lshw -short -C disk).
    2) If you're looking for newly mounted file systems, you could use check /proc/mounts file.
    3) A loop running continuously without pauses can eat a lot of resources needlessly. You can add sleep to mitigate that.

    Example:
    Code:
    #!/bin/bash
    
    i=$(wc -l /proc/mounts | cut -d' ' -f1) # get newlines count from /proc/mounts and assign to $i variable 
    while :; do
    	sleep 1 # pause for 1 second
    	j=$(wc -l /proc/mounts | cut -d' ' -f1)
    	[ $j -gt $i ] && echo "Found new mounted filesystem"
    	i=$j
    done
    Now, this is still far from perfect (as it's open to race conditions and whatnot) but it'll work most of the time.
    Last edited by prodigy_; May 23rd, 2013 at 04:26 PM.

  7. #7
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: shell script

    $a= ` expr $a + 1 `

    that doesn't look good either

    Code:
               no spaces here --v
    assignment          leftside=rightside
       << $ not on this side << | >> $ on this side >>
    when you see $X think 'get value of X' - that will make it obvious that $X=abc (assign abc to value of X?) doesn't make sense, but abc=$X does (assign value of X to abc).

    also forget about `` and use $( ) instead, you still have time to save your soul $( ) does the same but is 10x more readable (no doubt which part opens and which part closes, which can't be said about `` eg `a`b`c`d`e`f`g`) and can't be mistaken for a single quote at a glance.

    If you are going to use bash to write your scripts, you can use its (()) to do the integer math
    a=$( expr $a+1 ) is equivalent to (( a=a+1 )) or (( a++ )) or a=$(( a+1 ))
    Last edited by Vaphell; May 22nd, 2013 at 05:53 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  8. #8
    Join Date
    May 2013
    Beans
    48

    Re: shell script

    Hey thank u, got my problem solved.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •