Object-oriented shell for linux? [closed]

Asked
Active3 hr before
Viewed126 times

8 Answers

shelllinux
90%

Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers , 1 possible duplicate of what object-based shells are there? and Can anyone recommend a good modern alternative to bash? – Dennis Williamson Dec 21 '10 at 11:00 ,jq is not quite an object-oriented shell, but it provides some of the benefits which object-oriented shells may have; I use it a lot, together with shell scripts, for such tasks., 4 The usual scripting languages aren't very good at what shells are designed for: invoking programs, referencing files, plugging things together with pipes. – ak2 Dec 21 '10 at 8:00

For example:

#!/usr/local/bin/node

var sys = require('sys'),
   exec = require('child_process').exec;

// Run `ls`:
exec('ls -lh /usr', function(error, output, erroutput) {
   sys.print('output:    ' + output);
   sys.print('erroutput: ' + erroutput);
});
load more v
88%

scsh is a scheme implementation (i.e. a decent programming language) with the kind of system bindings traditionally found in unix shells (strings, processes, files). It doesn't aim to be usable as an interactive shell however.,Standard shell features: job control, readline editing and history, aliases, cat ls cd and pwd, pager integration, running any system command by prefixing it with a ! or enabling %rehashx, command output assignable to a python variable, python values available as shell variables.,hipersayanx has created an article Object Oriented Programming in Bash. Basically he hi-jacked $FUNCNAME,function, compgen, and export to create as close to OOP one can get in bash.,Unix shells tend to concentrate on the interactive aspect and subcontract most of the system access and some of the programming to external tools, such as:

So, you would might define a class like this:

class Queue N = 10 add = q_add remove = q_remove

Creating an instance of this Queue might be done like this:

class Q: Queue N = 100

or

inst Q: Queue N = 100

Adding items into this queue could be done like this:

$Q add 1 2 aaa bbb "a string"

Removing items into a variable X might be done like this:

$Q remove X

And dumping structure of an object could be done like this:

$Q dump

Which would return something like this:

Q {
   parent = Queue {
      parent = ROOT {
         this = ROOT
         0 = dispatch ROOT
      }
      class = Queue
      N = 10
      add = q_add
      remove = q_remove
      0 = dispatch Queue
   }
   class = Q
   N = 4
   add = q_add
   remove = q_remove
   0 = dispatch Q
   1 =
      2 = ccc ddd
   3 =
      4 =
}

Classes are created using a class function like this:

class() {
      local _name = "$1:"
      # append a: to handle
      case ofclass with no parent
      printf "$FUNCNAME: %s\n"
      $_name
      local _this _parent _p _key _val _members
      _this = $ {
         _name % %: *
      }
      # get class name
      _parent = $ {
         _name # *:
      }
      # get parent class name
      _parent = $ {
         _parent /: /}                        # remove handy :
         declare - g - A $_this # make class storage[[-n $_parent]] && {
            # copy parent class members into this class
            eval _members = \"\${!$_parent[*]}\"       # get indices of members
            for _key in $_members;do # inherit members from parent
            eval _val = \"\${$_parent[$_key]}\"    # get parent value
            eval $_this[$_key] = \"$_val\"         # set this member
            done
         }
         shift 1

         # overwrite with specific values
         for this object
         ROOT_set $_this "$@"
         "0=dispatch $_this"
         "parent=${_parent:-ROOT}"
         "class=$_this"
      }

Bash associative arrays have a quirk that makes this work neatly: $Q[0]} is identical to $Q. This means that we can use array name to call a method dispatch function:

dispatch() {
   local _this = $1 _method = $2 _fn
   shift 2
   _fn = "$_this[$_method]"
   # reference to method name
   $ {
      !_fn
   }
   $_this "$@"
}

To get and set members you might do something like this:

# basic set and get for key - value members
ROOT_set() {
   # $QOBJ set key = value
   local _this = $1 _exp _key _val
   shift
   for _exp in "$@";
   do
      _key = $ {
         _exp % %= *
      }
   _val = "${_exp#*=}"
   eval $_this[$_key] = \"$_val\"
   done
}

ROOT_get() {
   # $QOBJ get var = key
   local _this = $1 _exp _var _key
   shift
   for _exp in "$@";
   do
      _var = $ {
         _exp % %= *
      }
   _key = $ {
      _exp # *=
   }
   eval $_var = \"\${$_this[$_key]}\"
   done
}

NOTE: This is not required for OOP in bash, but it is nice to see how objects are made.

# dump any object
obj_dump(){                                      # obj_dump <object/class name>
    local _this=$1 _j _val _key; local -i _tab=${2:-(${#_this}+2)}  # add 2 for " {"
    _tab+=2                                      # hanging indent from {
    printf "%s {\n" $_this
    eval "_key=\"\${!$_this[*]}\""
    for _j in $_key; do                          # print all members
        eval "_val=\"\${$_this[\$_j]}\""
        case $_j in
            # special treatment for parent
            parent) printf "%*s%s=" $_tab "" $_j; ${!_val} dump $(( _tab+${#_j}+${#_val}+2 ));;
                 *) printf "%*s%s=%s\n" $_tab "" $_j "$_val";;
        esac
    done
    (( _tab-=2 ))
    printf "%*s}\n" $_tab ""
    return 0
}

Oh! and I manually define a ROOT class to simplify class function:

declare - gA ROOT = (\
   [this] = ROOT\[0] = "dispatch ROOT"\ [dump] = obj_dump\[set] = "ROOT_set"\ [get] = "ROOT_get"\
)

With a few queue functions I defined some classes like this:

class Queue\
   in = 0 out = 0 N = 10\
dump = obj_dump\
add = q_add\
empty = q_empty\
full = q_full\
peek = q_peek\
remove = q_remove

class RoughQueue: Queue\
N = 100\
shove = q_shove\
head_drop = q_head_drop

Created some Queue instances and made them work:

class Q: Queue N = 1000
$Q add aaa bbb "ccc ddd"
$Q peek X
$Q remove X
printf "X=%s\n"
"$X"
$Q remove X
printf "X=%s\n"
"$X"
$Q remove X
printf "X=%s\n"
"$X"

class R: RoughQueue N = 3
$R shove aa bb cc dd ee ff gg hh ii jj
$R dump
load more v
72%

Is there anything similar to Microsoft Powershell (an object-oriented shell built on the .NET framework) for Linux (possibly built on Java, GObject, or its own object type/nothing)?,jq is not quite an object-oriented shell, but it provides some of the benefits which object-oriented shells may have; I use it a lot, together with shell scripts, for such tasks.,edit: especially if similar to bash or powershell or cmd etc. syntax (=''standard'' shell syntax),Even though this question is pretty old, I think its worth mentioning that in August 2016 Microsoft made Powershell open-source and cross platform. Instructions for installation are on github.

For example:

#!/usr/local/bin/node

var sys = require('sys'),
   exec = require('child_process').exec;

// Run `ls`:
exec('ls -lh /usr', function(error, output, erroutput) {
   sys.print('output:    ' + output);
   sys.print('erroutput: ' + erroutput);
});
load more v
65%

There aren't much OOP shells for linux. Most are the interactive shells of programming languages like perl, python or ruby.,Then there is xiki which mixes cli with interactive GUI.,Personally I really like that everything is just text and there is no magic of any kind, both the piped programs and me see the same thing. I definitely wouldn't want everything to be JSON.,If you are using regexes to select fields then you are something horribly wrong. You should use either awk or in some cases even cut is enough.

For example, you can do this to get all of the fields on the model returned from ps

ps firefox | select *
load more v
75%

This works because of the aforementioned object-nature of piped data-streams in PowerShell. There is no need to cut apart strings by delimiters.,One of the very first differences to take note of when using PowerShell for the first time is semantic clarity.,you talk about abbreviation and memorization like it’s a bad thing that power shell has “fixed” and then immediately introduce cmdlets or whatever. That’s when I stopped reading.,I love jq, and I use it often, but the difference is that PowerShell actually takes input and outputs AS objects. Not everything in Bash is output in JSON (in fact, very few is). In PowerShell, full objects are piped.

One example: To get all files or child-items in a directory I tell PowerShell like this:

PS > Get - ChildItem

Directory: /home/Ozymandias42

Mode LastWriteTime Length Name
-- -- -- -- -- -- -- -- - -- -- -- -- --
d-- --14 / 04 / 2021 08: 11 Folder1
d-- --13 / 04 / 2021 11: 55 Folder2
load more v
40%

A command shell is a command-line interface to interact with and manipulate an computer's operating system. ,Output from a command execution can be displayed in a table/grid which can be interactively sorted and filtered and/or otherwise manipulated after command execution ends. ,^ The Show-Command cmdlet inspects the command definition and opens an interactive windows with a named input field for each parameter/switch ,PowerShell Out-GridView cmdlet displays data in an interactive window with interactive sorting and filtering.

Background execution[edit]

Background execution allows a shell to run a command in background. POSIX shells and other Unix shells allow background execution by using the & character at the end of command, and in PowerShell you can use Start-Process or Start-Job commands.

Start - Process

Background execution[edit]

Background execution allows a shell to run a command in background. POSIX shells and other Unix shells allow background execution by using the & character at the end of command, and in PowerShell you can use Start-Process or Start-Job commands.

Start - Job
load more v
22%

Using functions to perform repetitive tasks is an excellent way to create code reuse. This is an important part of modern object-oriented programming principles.,If you instead want to just terminate execution of the function, then there is way to come out of a defined function.,This has the effect of causing functions defined inside test.sh to be read and defined to the current shell as follows −,Following is an example where we pass two parameters Zara and Ali and then we capture and print these parameters in the function.

To declare a function, simply use the following syntax −

function_name() {
   list of commands
}
load more v
60%

These commands list or alter information about the system.,The Linux systems support on-screen messages to other users and world-wide electronic mail.,Programming General C C++ JAVA FORTRAN Other Languages ,These commands work only where the remote host permits such connections.

The Shell

The Linux command interpreter or shell is the program users interact with in a terminal emulation window. The terminal emulation window can be one in the workstation's Graphical User Interface mate-terminal on Linux. Alternatively, it can be an application such as SSH secure shell client or PuTTY on a Windows PC that's logged into Linux over the network.

mate - terminal

The Shell

The Linux command interpreter or shell is the program users interact with in a terminal emulation window. The terminal emulation window can be one in the workstation's Graphical User Interface mate-terminal on Linux. Alternatively, it can be an application such as SSH secure shell client or PuTTY on a Windows PC that's logged into Linux over the network.

SSH
load more v

Other "shell-linux" queries related to "Object-oriented shell for linux? [closed]"