Find the answer to your Linux question:
Results 1 to 10 of 10
when i enter an argument to a script, how can i control wheter it has value? if(it has value) print its value is .... else print null how can i ...
  1. #1
    Just Joined!
    Join Date
    Dec 2009
    Posts
    28

    if statement

    when i enter an argument to a script,
    how can i control wheter it has value?

    if(it has value)
    print its value is ....
    else
    print null


    how can i do it?

  2. #2
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    Using -z will check if the string length is less than 0:

    Introduction to if
    Linux User #453176

  3. #3
    Just Joined! sedbean's Avatar
    Join Date
    Jan 2010
    Posts
    3
    What kind of script are you talking about ? In C, strlen() should solve the problem.

  4. #4
    Linux Newbie
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    243
    In the classic Bourne shell (which is the "/bin/sh" program), or in Bourne shell scripts, you would do this:
    Code:
    > if [[ -z $a ]] ; then echo 'Hello, world!' ; else echo $a ; fi
    Hello, world!
    > if [[ -z $a ]] ; then echo 'Hello, world!' ; else echo $a ; fi
    Hello, world!
    > a=HeyYou
    > if [[ -z $a ]] ; then echo 'Hello, world!' ; else echo $a ; fi
    HeyYou
    Just remember, when talking about Linux, you don't usually just say "in shell scripting" because Linux does not require you to use the Bourne shell. Other shell environments which use the same syntax as the Bourne shell include Korn Shell "/bin/ksh", the C shell "/bin/csh", the more modern C shell "/bin/tcsh", or the Bourne Again shell "/bin/bash". The default on most Linux distributions is "/bin/bash". I know Ubuntu gives you only "/bin/sh" and "/bin/bash", and if you want the others, you need to install them yourself.

    But you can use a shell that has nothing to do with the classic Unix style Bourne shell. You can lookup your own username in the "/etc/passwd" file, and it should have your default login shell -- the shell program that is automatically run after you login through a console. You can set that shell to "/usr/bin/python", or an interactive "perl" script, or to the interactive "lua" interpreter, or you can set it to "/usr/bin/emacs".

    If you want to know more about bash, read the bash man page.

  5. #5
    Just Joined!
    Join Date
    Dec 2009
    Posts
    28
    Thank you for your explanation.
    i've learned linux recently.
    but i feel its power.

    it works true thanks.
    my real problem is about ispell

    when i enter three argument to a script which is coded.
    if the word is spelled correctly it will show 'you entered true word'
    if the word is misspelled, it will show 'you entered wrong word"
    if you cannot enter an argument, it will show you 'no argument and exit'

    how can i do it?

  6. #6
    Linux Newbie
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    243
    I don't really understand what you would like to do.

    Tools like aspell and ispell are designed to work on files, but not on command-line arguments. If you have an HTML file you can use "-h", or a LaTeX file you can use -t to parse the code and check the spelling of the relevant text.
    Code:
    ispell -h blog-entry.html
    when you do this, you should enter into an interactive console program that works just like a spell checker in your graphical text editor.

  7. #7
    Just Joined!
    Join Date
    Dec 2009
    Posts
    28
    Thanks for your answers.

    i only want when i enter a word from console for exapmple, forum
    it will return me 'yes, true word'
    but if i entered foram, it will return me 'you have entered false word'

  8. #8
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,316
    Are these HomeWork questions?

    Check here.
    It is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

  9. #9
    Linux Newbie
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    243
    I am also wondering if tihs is a homework question, because now your problem is getting a little bit more complicated. Well, I will go ahead and give you the answer anyway because I thought it was a fun exercise.

    Let me tell you first, however, in a perfect world, Linux tools will offer eactly what you need, for example spell checking of terms from the command line, but Linux is never perfect. Computer programmers cannot anticipate all needs of all people. So you have to be creative.

    I don't know of any way to make "ispell" solve your problem directly, but you can filter out only the lines you need with "grep". As you can see from the ispell manual page, it will output a "*", "+", "-", "&", "?", or "#" character as the very first character as the results of the spell check, where "*" means it is a correct spelling. So to make grep ony accept this kind of output:
    Code:
    > echo computer | ispell -a | grep -E '^[*+-&?#].*'
    *
    > echo computre | ispell -a | grep -E '^[*+-&?#].*'
    & computre 2 0: compute, computer
    I have used several common Unix/Linux tools, including pipes (the "|" character), and regular expressions (the input to the "grep -E" command). You should learn about these concepts. You should also learn about the "bash" shell environment, because it is a fairly powerful and commonly used shell environment. Bash lets you create functions, for example:
    Code:
    > function hello () { for NAME in $@ ; do echo "Hello, $NAME\!" ; done }
    > hello Brian Adam
    Hello, Brian!
    Hello, Adam!
    Bash also lets you work with strings, for example to output the characters 0 through 5 of the string "Hello, world!", you could do this:
    Code:
    > a='Hello, world' ; echo ${a:0:5}
    Hello
    Here is my solution to your problem:
    Code:
    > function checkspell () { for INPUT_ITEM in $@ ; do { CHECKED=$(echo $INPUT_ITEM | ispell -a | grep -E '^[*+-&?#].*') && b=${CHECKED:0:1} ; if [[ $b == '*' ]] ; then echo "$INPUT_ITEM -> OK" ; else echo "$INPUT_ITEM -> UNKNOWN WORD" ; fi } ; done }
    And now you can use the function in the command line like this:
    Code:
    > checkspell hello
    hello -> OK
    > checkspell hello world
    hello -> OK
    world -> OK
    > checkspell hello wordl
    hello -> OK
    wordl -> UNKNOWN WORD
    > checkspell computre
    computre -> UNKNOWN WORD
    There might be an easier way than this, but I don't know because I don't ever use spell checking on the command line and I have never needed to find such a tool. I think you already know, you will not always have a programmer like me available to write code for you like this, so do your best to learn about "bash", "grep", as well as the C programming language, and other helpful programming tools like GNU "make". (So no more free answers to homework questions!)

    One of the most important things in Linux is reading manual pages. You should try typing "man ispell", and "man bash" for starters. This usually gives me some ideas about how to solve my problems.

    Good luck!

  10. #10
    Just Joined!
    Join Date
    Dec 2009
    Posts
    28
    Thanks my friend. You are excellent.
    Thanks.

Posting Permissions

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