如果该脚本文件包含在另一个脚本文件中(这使得它与此问题不同),如何获取bash脚本文件所在的目录?
/script1.sh
. /dir2/script2.sh
Run Code Online (Sandbox Code Playgroud)
/dir2/script2.sh
# echoes "/dir2"
echo whatevergetsthatdir
Run Code Online (Sandbox Code Playgroud)
这是我试图"修复"的脚本
/etc/init.d/silvercar-gameserver(每个实例都是唯一的)
#!/bin/bash
#
# /etc/rc.d/init.d/silvercar-gameserver
#
# Initscript for silvercar game server
#
# chkconfig: 2345 20 80
# description: lalalalala
#CONFIG
BIN=/opt/silvercar-gameserver # Want to get rid of this
CONF=/etc/opt/silvercar-gameserver
. /etc/init.d/functions
. $BIN/gameserver.sh.inc
exit 0
Run Code Online (Sandbox Code Playgroud)
/opt/silvercar-gameserver/gameserver.sh.inc(每次安装都不得更改.在svn中)
# Meant to be included from a script in init.d
# Required input:
# CONF (e.g. /etc/opt/silvercarserver)
# -- Installation config (must provide JSVC, JAVA_HOME)
. …Run Code Online (Sandbox Code Playgroud) em.getTransaction().begin();
StringData sd = em.find(StringData.class, key);
System.out.println("Old value: " + sd.getData());
sd.setData(newValue);
// em.persist(sd);
em.getTransaction().commit();
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我没有打电话persist,它被注释掉了,因为我先干这个代码.然而,事实证明它并不是那么干燥.在检查数据库时,我看到数据已更改(幸运的是它是一个测试数据库).
显然我对Hibernate/JPA的理解是有缺陷的.是不是persist总是要求更改数据?如果没有,什么时候保存什么是什么规则?
在Java中就是这种情况:
public void method() {
if (condition) {
Object x = ....;
}
System.out.println(x); // Error: x unavailable
}
Run Code Online (Sandbox Code Playgroud)
我想知道的是:事实x是否仅限于if-statement 的范围只是Java编译器的一个特性,或者x实际上是在if-statement 之后从堆栈中删除了?
当您在Java中使用RMI时,异常的远程堆栈跟踪将在您收到时添加,有点像这样:
ERROR Client received error when doing stuff:
myapp.FooBarException: bla
at server.myMethod()
at rmi.callHandler() // and now, on the next line comes the client
at rmi.sendCall();
at client.doServerMethod()
at Thread.run()
Run Code Online (Sandbox Code Playgroud)
这种堆栈跟踪"伪造"怎么办?
我想要它是什么(除了被激活之外)?好吧,如果我能做到这一点,它会对我有所帮助:
outer() {
thread = new Thread(...
inner();
// inner() throws
// RuntimeException
// at inner();
// at Runnable.run();
// at Thread.run();
// at outer();
// at lalalala();
// ...
).start();
thread.join();
}
Run Code Online (Sandbox Code Playgroud)
而让这个抛出的异常inner()必须outer()(和方法降低下来链)的堆栈跟踪为好,可以进行日志记录.
我目前正在使用这样的代码向我的实体中的集添加新条目.
player = em.find(Player.class, playerId);
player.getAvatarAttributeOwnership().add(new AvatarAttributeOwnership(...));
Run Code Online (Sandbox Code Playgroud)
它有效,但每次我想添加一个项目时,整个集合都会被加载.
INSERT INTO AvatarAttributeOwnership(player, data, ...) VALUES({player}, ...);Set和AvatarAttributeOwnership.equals,但我相信,这将不再工作.我怎么能强制执行呢?我正在使用JPA2 + Hibernate.码:
@Entity
public class Player implements Serializable {
@Id
@GeneratedValue
private long id;
@ElementCollection(fetch=FetchType.LAZY)
// EDIT: answer to #2
@CollectionTable(uniqueConstraints=@UniqueConstraint(columnNames={"Player_id","gender","type","attrId"}))
Set<AvatarAttributeOwnership> ownedAvatarAttributes;
...
}
@Embeddable
public class AvatarAttributeOwnership implements Serializable {
@Column(nullable=false,length=6)
@Enumerated(EnumType.STRING)
private Gender gender;
@Column(nullable=false,length=20)
private String type;
@Column(nullable=false,length=50)
private String attrId;
@Column(nullable=false)
private Date since;
@Override
public boolean equals(Object obj) {
if (this == …Run Code Online (Sandbox Code Playgroud) 假设我在PHP中发送一些HTTP状态代码,我是否真的需要这样做
header('HTTP/1.1 301 Moved Permanently');
Run Code Online (Sandbox Code Playgroud)
还是足够了
header('HTTP/1.1 301 FooBar');
Run Code Online (Sandbox Code Playgroud)
我曾经在一个快速而又脏的反向代理中执行此操作,我只能从CURL响应中获取代码而不是消息,并将其FooBar作为消息发回.这似乎工作正常.
final List<Tuple> data =
em.createQuery("SELECT p.id AS i, p.membership AS m FROM Player p WHERE p.id IN :ids", Tuple.class)
.setParameter("ids", ids)
.getResultList();
Run Code Online (Sandbox Code Playgroud)
这给出了错误" Cannot create TypedQuery for query with more than one return".我可以通过省略type参数(并使用Object []而不是Tuple来解决这个问题,我后来发现):
@SuppressWarnings("unchecked")
final List<Object[]> data =
em.createQuery("SELECT p.id AS i, p.membership AS m FROM Player p WHERE p.id IN :ids")
.setParameter("ids", ids)
.getResultList();
Run Code Online (Sandbox Code Playgroud)
但是,是否有一个不需要未经检查的代码的解决方案?
在Java中解析类似shell的命令行的推荐方法是什么.我并不是说当它们已经是数组形式时处理选项(例如处理"-x"等),已经有很多问题和答案.
不,我的意思是将完整的命令字符串拆分为"令牌".我需要转换一个字符串,如:
user 123712378 suspend "They are \"bad guys\"" Or\ are\ they?
Run Code Online (Sandbox Code Playgroud)
...到列表/数组:
user
123712378
suspend
They are "bad guys"
Or are they?
Run Code Online (Sandbox Code Playgroud)
我目前只是在空白上进行拆分,但显然无法处理引号和转义空格.
(引用处理是最重要的.逃脱的空间会很棒)
注意:我的命令字符串是来自类似shell的Web界面的输入.它不是建立在main(String[] args)
我正在编写一个Java游戏引擎(http://victoryengine.org),我一直在尝试生成带有红色/蓝色眼镜的深度"3D"图像.我正在使用Java2D进行图形处理.
我创造了一些有效的东西,但速度很慢(通过手动复制像素值和类似的东西).
我需要的是两个BufferedImages(一个用于左眼,一个用于右侧)并将它们组合成一个(另一个缓冲区或直接连接到屏幕).对于一个我只想要红色通道,而另一个我想要绿色和蓝色通道.最快的方法是什么?
class Foo {
public function bar():void { ... }
}
var clazz:Class = Foo;
// ...enter the function (no Foo literal here)
var fun:Function = clazz["bar"]; // PROBLEM: returns null
// later
fun.call(new Foo(), ...);
Run Code Online (Sandbox Code Playgroud)
做上述事情的正确方法是什么?我想要做的Java相当于:
Method m = Foo.class.getMethod("bar", ...);
m.invoke(new Foo(), ...);
Run Code Online (Sandbox Code Playgroud)
实际代码(含解决方法):
class SerClass {
public var className:String;
public var name:String;
private var ser:String = null;
private var unser:Function = null;
public function SerClass(clazz:Class):void {
var type:XML = describeType(clazz);
className = type.@name;
// determine name
name = …Run Code Online (Sandbox Code Playgroud)