我正在尝试使用bash脚本来使脚本像电话簿一样, 所以我试图创建类和对象,但不幸的是我找不到办法做到这一点! 所以我问如何使用bash脚本创建一个类?
答案 0 :(得分:36)
您可以尝试做类似的事情
example.sh
#!/bin/bash
# include class header
. obj.h
. system.h
# create class object
obj myobject
# use object method
myobject.sayHello
# use object property
myobject.fileName = "file1"
system.stdout.printString "value is"
system.stdout.printValue myobject.fileName
obj.h
obj(){
. <(sed "s/obj/$1/g" obj.class)
}
obj.class
# Class named "obj" for bash Object
# property
obj_properties=()
# properties IDs
fileName=0
fileSize=1
obj.sayHello(){
echo Hello
}
obj.property(){
if [ "$2" == "=" ]
then
obj_properties[$1]=$3
else
echo ${obj_properties[$1]}
fi
}
obj.fileName(){
if [ "$1" == "=" ]
then
obj.property fileName = $2
else
obj.property fileName
fi
}
system.h中
. system.class
system.class
system.stdout.printValue(){
echo $($@)
}
system.stdout.printString(){
echo $@
}
参考链接:https://github.com/mnorin/bash-scripts/tree/master/objects 关键是你不能创建对象,但你可以在bash中模拟面向对象的编程
答案 1 :(得分:9)
Bash是一种脚本语言,不支持OOP,所以你不能。试试Python。
你唯一能做的就是拥有几个阵列,但这很麻烦。使用索引链接它们。
答案 2 :(得分:2)
所以我记得几年前检查过这个问题并回答了..并且在想...什么!?!!?!
然后上周,我仔细查看了@Maxims的答案,然后就很清楚了。
我花了上周的时间,为类对象创建了bash类编译器和类加载器,以及方法和其他好处。.所有这些都是我想要创建终端动画基础结构的原因:
因此,尽管这只是一个开始,但我发现这是一次超级酷而具有挑战性的冒险。.I hope my code would help someone else as well !!
BTW:仅在MAC OS上进行了测试,因此可能需要进行一些调整:)
答案 3 :(得分:1)
虽然没有真正的方法可以在Bash中创建类,但是您可以获得一些创意。多年来,我发现执行此操作的首选方法是创建一个函数,该函数返回可以执行以更改实例状态或读取实例属性的命令。实例数据可以存储在数组中。
例如,如果您想为二进制搜索树创建一个类,则可以在BinarySearchTree.sh
中使用它:
__BINARYSEARCHTREE_INSTANCE_DATA__=()
__BINARYSEARCHTREE_INSTANCE_DATA_LENGTH__=()
__BINARYSEARCHTREE_INSTANCE_DATA_SIZE__=()
BinarySearchTree()
{
echo "__BinarySearchTree__ $RANDOM "
}
__BinarySearchTree__()
{
local id="$1"
local code="$2"
case "$code" in
'.insert' | '[insert]' | "['insert']" | '["insert"]')
local value="$3"
if [ "${__BINARYSEARCHTREE_INSTANCE_DATA__["$id", 0] + set}" ]; then
local length="${__BINARYSEARCHTREE_INSTANCE_DATA_LENGTH__["$id"]}"
local new_node="$length"
__BINARYSEARCHTREE_INSTANCE_DATA__["$id", "$length"]="$value"
length=$((length + 1))
__BINARYSEARCHTREE_INSTANCE_DATA__["$id", "$length"]=''
length=$((length + 1))
__BINARYSEARCHTREE_INSTANCE_DATA__["$id", "$length"]=''
local current_node=0
local parent
while [ 1 ]; do
parent="$current_node"
if [ "$value" -lt "${__BINARYSEARCHTREE_INSTANCE_DATA__["$id", "$((current_node))"]}" ]; then
current_node="${__BINARYSEARCHTREE_INSTANCE_DATA__["$id", "$((current_node + 1))"]}"
if [ "$current_node" == '' ]; then
__BINARYSEARCHTREE_INSTANCE_DATA__["$id", "$((parent + 1))"]="$new_node"
break
fi
else
current_node="${__BINARYSEARCHTREE_INSTANCE_DATA__["$id", "$((current_node + 2))"]}"
if [ "$current_node" == '' ]; then
__BINARYSEARCHTREE_INSTANCE_DATA__["$id", "$((parent + 2))"]="$new_node"
break
fi
fi
done
__BINARYSEARCHTREE_INSTANCE_DATA_LENGTH__["$id"]="$((length + 1))"
__BINARYSEARCHTREE_INSTANCE_DATA_SIZE__["$id"]="$((${__BINARYSEARCHTREE_INSTANCE_DATA_SIZE__["$id"]} + 1))"
else
__BINARYSEARCHTREE_INSTANCE_DATA__["$id", 0]="$value"
__BINARYSEARCHTREE_INSTANCE_DATA__["$id", 1]=''
__BINARYSEARCHTREE_INSTANCE_DATA__["$id", 2]=''
__BINARYSEARCHTREE_INSTANCE_DATA_LENGTH__["$id"]=3
__BINARYSEARCHTREE_INSTANCE_DATA_SIZE__["$id"]=1
fi;;
'.has' | '[has]' | "['has']" | '["has"]')
local value="$3"
local current_node=0
if [ "${__BINARYSEARCHTREE_INSTANCE_DATA__["$id", 0] + set}" ]; then
while [ 1 ]; do
local current_value="${__BINARYSEARCHTREE_INSTANCE_DATA__["$id", "$((current_node))"]}"
if [ "$current_value" == "$value" ]; then
return 0
fi
if [ "$value" -lt "$current_value" ]; then
current_node=${__BINARYSEARCHTREE_INSTANCE_DATA__["$id", "$((current_node + 1))"]}
else
current_node=${__BINARYSEARCHTREE_INSTANCE_DATA__["$id", "$((current_node + 2))"]}
fi
if [ "$current_node" == '' ]; then
return 1
fi
done
else
return 1
fi;;
'.size' | '[size]' | "['size']" | '["size"]')
if [ "${__BINARYSEARCHTREE_INSTANCE_DATA__["$id", 0] + set}" ]; then
echo "${__BINARYSEARCHTREE_INSTANCE_DATA_SIZE__["$id"]}"
else
echo 0
fi;;
'.empty' | '[empty]' | "['empty']" | '["empty"]')
if [ "${__BINARYSEARCHTREE_INSTANCE_DATA__["$id", 0] + set}" ]; then
return 1
else
return 0
fi;;
'.clear' | '[clear]' | "['clear']" | '["clear"]')
unset "__BINARYSEARCHTREE_INSTANCE_DATA__[$id, 0]"
esac
}
然后制作这样的对象:
source './BinarySearchTree.sh'
process()
{
local tree="$1"
$tree.insert 52
$tree.insert -150
$tree.insert 42
if $tree.has 42; then
echo 'Has 42!'
else
echo 'Does not have 42!'
fi
$tree.clear
echo "Size: $($tree.size)"
}
main()
{
local tree=$(BinarySearchTree)
process "$tree"
}
main "$#" "$@"
此方法的优点是可以将对象传递到其他函数中,并且没有外部文件操作。尽管这似乎不切实际,但实际上可以使Bash成为一种不错的语言,因为您可以模块化您的类。
答案 4 :(得分:0)
尝试使用BashX:https://github.com/reduardo7/bashx
#!/usr/bin/env bash
# ...
@Actions.action1() { # \\n Action without arguments
set -x
pwd
@log "
Action 1
Multi-Line
"
ls -la
bash
}
@Actions.action2() { # param1 [param2] \\n Action with arguments\\n\\tdescription second line\\nother line
eval "$(@user.options 'new:-n|-N' 'path:-p|--path:true')"
set -x
@log "'n' or 'N' parameter: ${user_options_new}"
@log "'p' or 'path' parameter: ${user_options_path[@]} (${#user_options_path[@]})"
local param1="$1"
local param2="$2"
[[ "$param1" != 'asd' ]] && @throw.invalidParam param1
@log Action 2
@log Param1: $1
@log Param2: $2
}
@app.run "$@"
./myscript action1
./myscript action2 -n -p /tmp 'my param 1'