Thread: bash to cpp

  1. #1
    Registered User setevoy's Avatar
    Join Date
    Jul 2014
    Location
    Kiev, UA
    Posts
    4

    bash to cpp

    Hi.

    I'm new in C++ and sure - I'm learning it.

    But - just interesting - how next bash function can't looks like in C++?

    This code used to check array of system global variables:
    Code:
    #!/usr/bin/env bash
    
    # defining and fill array
    variables=( [1]=$sysvar1 
                [2]=$sysvar2 
                [3]=$sysvar3 
                [4]=$sysvar3 
              )
    
    # function 
    checkvars () {
    
    local var
    
    # run loop for list of arguments one-by-one
    for var in "$@"
    do
      # test - if $var not empty will print "Var exist"; else - "Empty var"
      [[ "$var" ]] && echo "Var exist: $var;" \
        || echo "Empty var!"
    done
    }
    
    # start function with array as argument
    checkvars "${variables[@]}"
    It's simple enough and I hope C++ code will not be too complicated for me to understand it :-)

    Thanks.

    P.S. Goal of post - not really made some job for me - but show an example of some C++ code, which I'll can sort out and compare with bash code.
    Some explanations will be nice.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There is no notion of system/environment variables in standard C++. Since you are using bash, you are presumably using *nix, so read the manual entry on getenv.

    EDIT:
    Oh wait, I'm mistaken: turns out that getenv is indeed part of the C standard library, and hence part of the C++ standard library. Still the manual entry will tell you how to use it, though you should #include <cstdlib> rather than <stdlib.h> and use std::getenv rather than just getenv.
    Last edited by laserlight; 07-21-2014 at 03:07 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User setevoy's Avatar
    Join Date
    Jul 2014
    Location
    Kiev, UA
    Posts
    4
    Thanks for tip, @laserlight .

    Yes, I'm using Linux.

    About: and use std::getenv rather than just getenv

    Code:
    $ cppman -f std::getenv
    error: std::getenv: nothing appropriate.
    $ cppman -f getenv
    getenv
    fegetenv
    Do you mean 'getenv'?

    From manual - it's look like correct:

    Code:
    NAME
           getenv - Get environment string
    
    
    TYPE
           function
    
    
    SYNOPSIS
           #include <cstdlib>
    
    
           char* getenv (const char* name);

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by setevoy
    Do you mean 'getenv'?
    No, std::getenv. Try std::cout in your cppman tool. If it gives a similiar response, then your cppman tool is merely unable to recognise the qualified name. If it gives a different response, then your cppman tool is lacking data.

    Quote Originally Posted by setevoy
    From manual - it's look like correct:
    The manual entry probably omitted the qualification with the assumption that the reader knows that the name belongs in the std namespace.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User setevoy's Avatar
    Join Date
    Jul 2014
    Location
    Kiev, UA
    Posts
    4
    Usually - `cppman` can recognize `std` namespace:

    Code:
    $ cppman -f std::cin
    std::cin
    It takes data from cplusplus.com - The C++ Resources Network.

    Anyway - thanks, now I have one way for searching :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bash troubles
    By Annonymous in forum Linux Programming
    Replies: 1
    Last Post: 09-24-2012, 01:09 AM
  2. Can't run Bash!
    By Yarin in forum Tech Board
    Replies: 3
    Last Post: 05-11-2009, 07:25 PM
  3. Help set gvim bash
    By glo in forum Tech Board
    Replies: 3
    Last Post: 04-04-2008, 07:44 AM
  4. What company to bash..?
    By h_howee in forum A Brief History of Cprogramming.com
    Replies: 74
    Last Post: 02-03-2008, 01:13 PM
  5. BASH scripting.
    By Kennedy in forum Tech Board
    Replies: 6
    Last Post: 05-06-2007, 06:13 PM