使用变量获取第7个参数的bash脚本

时间:2017-06-11 20:33:11

标签: arrays linux bash ubuntu

我试图通过$ x而不是7美元获得脚本的第7个参数,这可能吗?

set -- $args
echo $(($x))

因此需要打印7美元,因为$ x = 7

1 个答案:

答案 0 :(得分:0)

除了public class PlayGameCountry extends AppCompatActivity implements View.OnClickListener { final static long INTERVAL = 1; // 1 second final static long TIMEOUT = 7; // 1 second CountDownTimer countDownTimer; int progressValue = 0; int score = 0, index = 0, thisQuestion = 0, correctAnswer, totalQuestions; List<Questions> questionsList = new ArrayList<>(); String mode; ProgressBar progressBar; ImageView flagCountry; TextView scoreText, numberQuestion; Button answerA, answerB, answerC, answerD; WorldCountryDatabase worldCountryDatabase; ChoiceGame choiceGame; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_play_game_country); // Get data from ChoiceGame Bundle bundle = getIntent().getExtras(); if (bundle != null) mode = bundle.getString("Mode"); worldCountryDatabase = new WorldCountryDatabase(this); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); if (getSupportActionBar() != null) { getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } flagCountry = (ImageView) findViewById(R.id.flagQuiz); scoreText = (TextView) findViewById(R.id.scoreText); numberQuestion = (TextView) findViewById(R.id.trueAnswer); answerA = (Button) findViewById(R.id.firstAnswer); answerB = (Button) findViewById(R.id.secondAnswer); answerC = (Button) findViewById(R.id.thirthAnswer); answerD = (Button) findViewById(R.id.forthAnswer); progressBar = (ProgressBar) findViewById(R.id.progressUser); answerA.setOnClickListener(this); answerB.setOnClickListener(this); answerC.setOnClickListener(this); answerD.setOnClickListener(this); } @Override protected void onResume() { super.onResume(); questionsList = this.getQuestionMode(mode); assert questionsList != null; totalQuestions = questionsList.size(); countDownTimer = new CountDownTimer(INTERVAL, TIMEOUT) { @Override public void onTick(long millisUntilFinished) { progressBar.setProgress(progressValue); progressValue++; } @Override public void onFinish() { countDownTimer.cancel(); showQuestion(++index); } }; showQuestion(index); } private void showQuestion(int index) { if (index < totalQuestions) { thisQuestion++; numberQuestion.setText(String.format("%d/%d", thisQuestion, totalQuestions)); progressBar.setProgress(0); progressValue = 0; int ImageId = this.getResources().getIdentifier(questionsList.get(index).getImage().toLowerCase(), "drawable", getPackageName()); flagCountry.setBackgroundResource(ImageId); answerA.setText(questionsList.get(index).getAnswerA()); answerB.setText(questionsList.get(index).getAnswerB()); answerC.setText(questionsList.get(index).getAnswerC()); answerD.setText(questionsList.get(index).getAnswerD()); countDownTimer.start(); } else { Intent scoreIntent = new Intent(getApplicationContext(), Done.class); Bundle bundle = new Bundle(); bundle.putInt("SCORE", score); bundle.putInt("TOTAL", totalQuestions); bundle.putInt("CORRECT", correctAnswer); scoreIntent.putExtras(bundle); startActivity(scoreIntent); finish(); } } @Override public void onClick(View v) { countDownTimer.cancel(); if (index < totalQuestions) { Button clickedButton = (Button) v; if (clickedButton.getText().equals(questionsList.get(index).getCorrectAnswer())) { score += 10; // increase score correctAnswer++; //increase correct answer showQuestion(++index); } else showQuestion(++index); // If choose right , just go to next question scoreText.setText(String.format("%d", score)); } } private List<Questions> getQuestionMode(String mode) { List<Questions> questionList = new ArrayList<>(); worldCountryDatabase = new WorldCountryDatabase(this); try { worldCountryDatabase.createDatabase(); worldCountryDatabase.openDataBase(); } catch (Exception e) { e.printStackTrace(); } int limit = 0; if (mode.equals(Common.MODE.EASY.toString())) limit = 30; else if (mode.equals(Common.MODE.MEDIUM.toString())) limit = 50; else if (mode.equals(Common.MODE.HARD.toString())) limit = 100; else if (mode.equals(Common.MODE.HARDEST.toString())) limit = 200; try { Cursor cursor = worldCountryDatabase.QueryData(String.format("SELECT * FROM country ORDER BY Random() LIMIT %d", limit)); if (cursor == null) return null; if (cursor.moveToNext()) { do { int Id = cursor.getInt(cursor.getColumnIndex("id")); String Image = cursor.getString(cursor.getColumnIndex("Image")); String AnswerA = cursor.getString(cursor.getColumnIndex("AnswerA")); String AnswerB = cursor.getString(cursor.getColumnIndex("AnswerB")); String AnswerC = cursor.getString(cursor.getColumnIndex("AnswerC")); String AnswerD = cursor.getString(cursor.getColumnIndex("AnswerD")); String CorrectAnswer = cursor.getString(cursor.getColumnIndex("CorrectAnswer")); Questions question = new Questions(Id, Image, AnswerA, AnswerB, AnswerC, AnswerD, CorrectAnswer); questionList.add(question); } while (cursor.moveToNext()); worldCountryDatabase.close(); } } catch (Exception e) { e.printStackTrace(); } return questionList; } 不是那样。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class testReadQuotes {


    public static void main(String args[]) throws IOException{

        Pattern patt = Pattern.compile("\"([^\"]*)\"");
        BufferedReader r = new BufferedReader(new FileReader("src\\files\\myFile.txt"));

        String line;

        while ((line = r.readLine()) != null) {

          Matcher m = patt.matcher(line);

          while (m.find()) {
            System.out.println(m.group(0));
          }

        }

    }

}