我有一个shell脚本。现在我想使用java在此脚本中传递NAME
和PASS
。然后我想用java
执行它。下面我在这里写了我的shell
脚本以及java代码。
请帮帮我。
#!/bin/bash
# Create ftp user, create folders and set permissions
# Usage: ./create_ftp_user.sh [username] "[password]"
#
NAME=bimal
PASS=bimal
echo "USAGE: create_ftp_user.sh [username] [password]"
# check input parameters
if [ -z "$NAME" ]; then
echo "Error: username is not set"
exit
fi
if [ -z "$PASS" ]; then
echo "Error: password not set"
exit
fi
# create system user
echo "Creating user: $NAME"
echo "With password: $PASS"
useradd -p `openssl passwd -1 $PASS` -m $NAME -g ftpaccess -s /usr/sbin/nologin
# save to users log
echo "user: $NAME, pass: $PASS" >> new_ftp_users_list
# add user to ftp daemon list
echo "$NAME" >> /etc/vsftpd/chroot_list
# create user ftp dir
mkdir /var/ftpupload/$NAME
# Set Ownership
chown $NAME: /var/ftpupload/$NAME
# Set permissions
chmod 0777 /var/ftpupload/$NAME
# restart vsftp daemon
#/etc/init.d/vsftpd restart
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package testscript;
/**
*
* @author deepak
*/
import java.io.IOException;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
public class TestScript {
int iExitValue;
String sCommandString;
public void runScript(String command){
sCommandString = command;
CommandLine oCmdLine = CommandLine.parse(sCommandString);
DefaultExecutor oDefaultExecutor = new DefaultExecutor();
oDefaultExecutor.setExitValue(0);
try {
iExitValue = oDefaultExecutor.execute(oCmdLine);
} catch (ExecuteException e) {
// TODO Auto-generated catch block
System.err.println("Execution failed.");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.println("permission denied.");
e.printStackTrace();
}
}
public static void main(String args[]){
TestScript testScript = new TestScript();
testScript.runScript("gksudo sh /home/deepak/Desktop/ftpusers.sh");
}
}
答案 0 :(得分:2)
CommandLine
可用于设置shell参数:
CommandLine oCmdLine = new CommandLine(sCommandString);
oCmdLine.addArgument("bimal");
oCmdLine.addArgument("bimalPassword");
您可以在Apache CommandLine文档页面上查看各种选项。
完成后,参数将在shell脚本中以$ 1和$ 2 位置shell参数的形式提供:
NAME=$1
PASS=$2