use Linux Write a shell script that displays the value of HOMEPAT H SHLVL, and LOGNAME variables in the following format: HOME= ———————— PATH= ———————— SHLVL= ———————— LOGNAME=————————
时间: 2023-07-27 09:07:24 浏览: 52
Sure, here's a shell script that will display the values of the HOMEPATH, PATH, SHLVL, and LOGNAME variables in the required format:
```
#!/bin/bash
echo "HOME= ${HOMEPATH}"
echo "PATH= ${PATH}"
echo "SHLVL= ${SHLVL}"
echo "LOGNAME= ${LOGNAME}"
```
Save this script with a file extension of `.sh`, and make it executable using the command `chmod +x script_name.sh`. Then, simply run the script using `./script_name.sh` and it will display the values of the specified variables in the required format.
相关问题
Write a program that counts the occurrences of words in a text and displays the words and their occurrences in alphabetical order of words. The program uses a TreeMap to store an entry consisting a wo
rd and its count. Here's an example program in Java:
```java
import java.util.Scanner;
import java.util.TreeMap;
public class WordCounter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
TreeMap<String, Integer> wordCountMap = new TreeMap<>();
System.out.println("Enter text (press Ctrl+D to end):");
while (scanner.hasNext()) {
String word = scanner.next().toLowerCase().replaceAll("[^a-z]+", "");
if (!word.isEmpty()) {
int count = wordCountMap.getOrDefault(word, 0);
wordCountMap.put(word, count + 1);
}
}
System.out.println("Word count:");
for (String word : wordCountMap.keySet()) {
System.out.println(word + " " + wordCountMap.get(word));
}
}
}
```
The program reads text from standard input using a `Scanner` and stores the word counts in a `TreeMap<String, Integer>`. Each word is converted to lowercase and stripped of non-alphabetic characters using a regular expression. The `getOrDefault` method is used to retrieve the current count for a word, and `put` is used to update the count. Finally, the program outputs the word counts in alphabetical order of words using a `for` loop over the key set of the `TreeMap`.
(Sort ArrayList) Write the following method that sorts an ArrayList of numbers: public static void sort(ArrayList<Integer> list) Write a test program that prompts the user to enter five numbers, stores them in an array list, and displays them in increasing order.
Here's the implementation of the method and the test program:
```java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class SortArrayList {
public static void sort(ArrayList<Integer> list) {
Collections.sort(list);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<>();
System.out.print("Enter 5 numbers: ");
for (int i = 0; i < 5; i++) {
list.add(input.nextInt());
}
sort(list);
System.out.println("Sorted numbers: " + list);
}
}
```
In the `sort` method, we use the `Collections.sort` method to sort the list in increasing order.
In the `main` method, we prompt the user to enter 5 numbers and store them in the `ArrayList` named `list`. Then we call the `sort` method to sort the list and display the sorted numbers.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)