Prepare a bash script which will take two arguments. The first argument is a file name and the second is one of commands: words, letters, sentences. Depending on the command, the script should count words, letters or sentences in the input file. On output the script should answer with a full sentence what is the result.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here is a bash script that fulfills your requirements:
bash
#!/bin/bash
# Check if the correct number of arguments is provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <file_name> <command>"
exit 1
fi
# Store the arguments in variables
file_name=$1
command=$2
# Check if the file exists
if [ ! -f "$file_name" ]; then
echo "Error: File '$file_name' not found!"
exit 1
fi
# Perform the requested command
case $command in
words)
count=$(grep -o -E '\w+' "$file_name" | wc -w)
echo "The file '$file_name' contains $count words."
;;
letters)
count=$(grep -o -E '\w' "$file_name" | wc -l)
echo "The file '$file_name' contains $count letters."
;;
sentences)
count=$(grep -o -E '\.' "$file_name" | wc -l)
echo "The file '$file_name' contains $count sentences."
;;
*)
echo "Error: Invalid command '$command'. Please choose 'words', 'letters', or 'sentences'."
exit 1
;;
esac
Save the script in a file, for example, count.sh. Then make the file executable with the command chmod +x count.sh.
To use the script, provide the file name as the first argument and the desired command as the second argument. For example:
bash
./count.sh input.txt words
This will count the number of words in the input.txt file and display the result.
Note: The script uses grep and wc commands to perform the counting. It assumes that the input file contains text only.
User
Prepare a bash script which will take two arguments. The first argument is a file name and the second is one of commands: words, letters, sentences. Depending on the command, the script should count words, letters or sentences in the input file. On output the script should answer with a full sentence what is the result. Accept a third input parameter which will indicate the language of the script. There may be two languages: English as a default and Polish. In case the third parameter equals Polish then the script must return the answer in Polish. At the end explain the script in Polish.
Assistant
Oto skrypt bash, który spełnia twoje wymagania:
bash
#!/bin/bash
# Sprawdź, czy podano poprawną liczbę argumentów
if [ "$#" -ne 3 ]; then
echo "Użycie: $0 <nazwa_pliku> <komenda> <język>"
exit 1
fi
# Przechowaj argumenty w zmiennych
file_name=$1
command=$2
language=$3
# Sprawdź, czy plik istnieje
if [ ! -f "$file_name" ]; then
echo "Błąd: Plik '$file_name' nie został znaleziony!"
exit 1
fi
# Wykonaj żądaną komendę
case $command in
words)
count=$(grep -o -E '\w+' "$file_name" | wc -w)
if [ "$language" == "Polish" ]; then
echo "Plik '$file_name' zawiera $count słów."
else
echo "The file '$file_name' contains $count words."
fi
;;
letters)
count=$(grep -o -E '\w' "$file_name" | wc -l)
if [ "$language" == "Polish" ]; then
echo "Plik '$file_name' zawiera $count liter."
else
echo "The file '$file_name' contains $count letters."
fi
;;
sentences)
count=$(grep -o -E '\.' "$file_name" | wc -l)
if [ "$language" == "Polish" ]; then
echo "Plik '$file_name' zawiera $count zdań."
else
echo "The file '$file_name' contains $count sentences."
fi
;;
*)
echo "Błąd: Nieprawidłowa komenda '$command'. Proszę wybrać 'words', 'letters' lub 'sentences'."
exit 1
;;
esac
Zapisz skrypt w pliku, na przykład count.sh. Następnie nadaj plikowi uprawnienia do wykonywania za pomocą polecenia chmod +x count.sh.