如果目录不为空,我正在尝试从目录中移动文件。该脚本已添加到import android.app.Dialog;
import android.os.CountDownTimer;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import java.util.HashMap;
import java.util.Map;
import holder.QuestionHolder;
import model.ExamModel;
import model.QuestionModel;
import model.ResultCardModel;
import model.ResultModel;
public class LiveExamActivity extends AppCompatActivity {
CardView alert_card, timer_card, exam_card;
Button start_exam, finish_exam_btn;
TextView timer, exam_name, alert_text;
DatabaseReference database;
ExamModel examModel;
FirebaseRecyclerAdapter adapter;
RecyclerView questionrecycler;
long correct_ans=0;
CountDownTimer cdtimer;
String stun;
Map<String, String> map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_exam);
database = FirebaseDatabase.getInstance().getReference();
alert_card = findViewById(R.id.alert_card);
start_exam = findViewById(R.id.start_exam_btn);
start_exam.setVisibility(View.GONE);
timer = findViewById(R.id.count_down_timer);
timer_card = findViewById(R.id.timer_card);
map = new HashMap<>();
exam_card = findViewById(R.id.exam_card);
exam_name = findViewById(R.id.exam_names);
alert_text = findViewById(R.id.alert_text);
finish_exam_btn = findViewById(R.id.finish_exam_btn);
questionrecycler = findViewById(R.id.questionrecycler);
finish_exam_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadResult();
}
});
exam_name.setText("Exam Name: "+getIntent().getStringExtra("exam_name"));
database.child("exams")
.orderByChild("name")
.equalTo(getIntent().getStringExtra("exam_name"))
.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
examModel = dataSnapshot.getValue(ExamModel.class);
alert_text.setText("You will get "
+examModel.getTotal_question()
+" questions to solve in "
+examModel.getExam_duration()
+" minutes");
start_exam.setVisibility(View.VISIBLE);
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
start_exam.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
initalizeExam();
alert_card.setVisibility(View.GONE);
timer_card.setVisibility(View.VISIBLE);
questionrecycler.setVisibility(View.VISIBLE);
finish_exam_btn.setVisibility(View.VISIBLE);
cdtimer = new CountDownTimer(examModel.getExam_duration()*1000*60, 1000) {
public void onTick(long millisUntilFinished) {
timer.setText(""+((millisUntilFinished / 1000)%36000)/60+"m "+(millisUntilFinished / 1000)%60+"s");
}
public void onFinish() {
timer.setText("done!");
uploadResult();
}
}.start();
}
});
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("questions")
.child(getIntent().getStringExtra("exam_name"));
FirebaseRecyclerOptions<QuestionModel> options =
new FirebaseRecyclerOptions.Builder<QuestionModel>()
.setQuery(query, QuestionModel.class)
.build();
adapter = new FirebaseRecyclerAdapter<QuestionModel, QuestionHolder>(options) {
@Override
public QuestionHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.live_question_card_recycler, parent, false);
return new QuestionHolder(view);
}
@Override
protected void onBindViewHolder(final QuestionHolder holder, int position, final QuestionModel model) {
View.OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(((RadioButton) v).getText().toString().equals(model.getAns())){
map.put(model.getQuestion(), model.getAns());
}
else {
map.values().remove(model.getAns());
}
}
};
holder.clue1.setText(model.getClue1());
holder.clue1.setOnClickListener(mOnClickListener);
holder.clue2.setText(model.getClue2());
holder.clue2.setOnClickListener(mOnClickListener);
holder.clue3.setText(model.getClue3());
holder.clue3.setOnClickListener(mOnClickListener);
holder.clue4.setText(model.getClue4());
holder.clue4.setOnClickListener(mOnClickListener);
holder.question.setText(model.getQuestion());
}
};
questionrecycler.setAdapter(adapter);
questionrecycler.setLayoutManager(new LinearLayoutManager(this));
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
private void uploadResult(){
try{
cdtimer.cancel();
}
catch (Exception e){}
final ResultModel resultModel = new ResultModel();
correct_ans = map.size();
resultModel.setTotal_ans(correct_ans);
resultModel.setTotal_questions(examModel.getTotal_question());
float percentage = (float) correct_ans/(float) examModel.getTotal_question();
percentage = percentage*30;
resultModel.setMarks_percentage(percentage);
resultModel.setName(stun);
Map map = new HashMap();
map.put("results/"+getIntent().getStringExtra("exam_name")+"/"+FirebaseAuth.getInstance().getCurrentUser().getUid(),resultModel);
ResultCardModel resultCardModel = new ResultCardModel();
resultCardModel.setObtained_marks(percentage);
resultCardModel.setTotal_ans(correct_ans);
resultCardModel.setTotal_questions(examModel.getTotal_question());
resultCardModel.setExam_name(examModel.getName());
map.put("resultcard/"+FirebaseAuth.getInstance().getCurrentUser().getUid()+"/"+getIntent().getStringExtra("exam_name"),resultCardModel);
database.updateChildren(map).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(LiveExamActivity.this, "Finished Exam!", Toast.LENGTH_SHORT).show();
finish();
}
});
}
@Override
public void onBackPressed() {
if (timer_card.getVisibility()== View.VISIBLE){
final Dialog dialog = new Dialog(LiveExamActivity.this);
dialog.setCancelable(true);
dialog.setContentView(R.layout.confirn_exit);
Button exit = dialog.findViewById(R.id.confirm);
exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadResult();
}
});
Button stay = dialog.findViewById(R.id.stay);
stay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
else super.onBackPressed();
}
private void initalizeExam(){
final ResultModel resultModel = new ResultModel();
resultModel.setTotal_ans(correct_ans);
resultModel.setTotal_questions(examModel.getTotal_question());
float percentage = (float) correct_ans/(float) examModel.getTotal_question();
percentage = percentage*100;
resultModel.setMarks_percentage(percentage);
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
FirebaseDatabase.getInstance()
.getReference()
.child("students")
.child(uid).child("name").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
stun = dataSnapshot.getValue(String.class);
resultModel.setName(dataSnapshot.getValue(String.class));
Map map = new HashMap();
map.put("results/"+getIntent().getStringExtra("exam_name")+"/"+FirebaseAuth.getInstance().getCurrentUser().getUid(),resultModel);
/*database.child("results")
.child(getIntent().getStringExtra("exam_name"))
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(resultModel);*/
final ResultCardModel resultCardModel = new ResultCardModel();
resultCardModel.setObtained_marks(0);
resultCardModel.setTotal_ans(0);
resultCardModel.setExam_name(examModel.getName());
resultCardModel.setTotal_questions(examModel.getTotal_question());
map.put("resultcard/"+FirebaseAuth.getInstance().getCurrentUser().getUid()+"/"+getIntent().getStringExtra("exam_name"),resultCardModel);
database.updateChildren(map);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
中,但是无论是否存在文件,它始终在执行吗?这件事怎么了?
cronjob
答案 0 :(得分:1)
Don't parse the output of ls
.您不需要任何外部命令即可对目录中的文件进行计数。外壳程序可以自己做。
尝试创建一个辅助函数,该函数检查传递给它的参数数量。然后让外壳扩展全局"$tempLogFolder"/*.log
。不需要ls
或grep
。唯一的技巧是启用nullglob
选项,因此,如果不存在文件,则全局扩展将扩展为空。
files_exist() { (($# > 0)); }
shopt -s nullglob
if files_exist "$tempLogFolder"/*.log; then
mkdir -p "$logFolder"
mv "$tempLogFolder"/*.log "$logFolder"/
fi
答案 1 :(得分:-1)
grep接受正则表达式,而不是glob。如果要使用*.log
之类的glob,请将其放在ls
之类的ls .... *.log
中(使用ls输出不是100%安全的)。
如果您想使用grep,请使用regex,我想您的意思是\.log$
。
如果要检查grep是否找到匹配项,则应检查$?
的返回码(grep
),而不要使用-z
测试。
如果将其放在crontab中,该脚本将始终执行。
答案 2 :(得分:-1)
我想你想要
if [ -n "$(ls -A $tempLogFolder | grep *.log)" ]; then
...
因为您只对返回的字符串的长度是否为非零感兴趣。