Doubt with exercise in Shellscript

Asked

Viewed 209 times

0

I would like some help to resolve these exercises:

In the Ex4.1.sh script add the option to enter the word and file name directly from the command line this way:

$  ./ex4.1.sh  <palavra>  <arquivo> 

In the Ex4.1.sh script the result of the "grep" command is shown on the screen. Change the script in such a way that this result does not appear on the screen. Write a script that tests whether the value passed as a day, month and year parameter is a valid date.

The script in the case is this one:

#!/bin/bash
# ex4.1.sh - teste de desvio
# Script para verificacao de estrutura de desvio
# localiza uma string em um arquivo
echo -n "Digite a palavra a ser localizada: "
read palavra
echo -n "Qual o arquivo a procurar?: "
read arquivo
if grep $palavra $arquivo
 then
   echo "$palavra encontrada no arquivo $arquivo"
   sleep 5
 else
   echo "$palavra não encontrada no arquivo $arquivo"
   sleep 5
 fi

Hello fedorqui colleague I managed to solve this example you gave, the problem is I was making it as a script with the code typed in gedit when it was to type directly in the command prompt of linux, there is the result

$ date -d "holaaa" 2> /dev/null && echo "Correct" || echo "Incorrect"

Incorrect

$ date -d "06 Dec 2017" 2> /dev/null && echo "Correct" || echo "Incorrect"

Wednesday Dec 6 00:00:00 AMT 2017

Correct

1 answer

2


In the Ex4.1.sh script add the option to enter the word and file name directly from the command line this way:

Picks up values with $1, $2...:

palavra=$1
arquivo=$2

Thus, $palavra contains the first and $arquivo the second:

With script.sh:

#!/bin/bash

palavra=$1
arquivo=$2
echo "palavra: $palavra. arquivo: $arquivo"

I’m gonna run:

$ ./script.sh "uma palavra" "hola.txt"
palavra: uma palavra. arquivo: hola.txt

Change the script in such a way that this result does not appear on the screen

Takes the results in a variable:

res=$(grep "$palavra" "$arquivo")

Write a script that tests whether the value passed as a day, month and year parameter is a valid date.

Usa date -d"<fecha>":

$ date -d"holaaa" && echo "correcto" || echo "incorreto"
date: invalid date ‘holaaa’
incorreto
$ date -d"23 Mar 2017" && echo "correcto" || echo "incorreto"
Thu Mar 23 00:00:00 CET 2017
correcto
  • 1

    Thank you so much for your help.

  • I am not able to do exercise 3, I am receiving command error messages not found, I did the script like this: #!/bin/bash # Ex4.7.sh - date test # Date check script $ date -d "<holaaa>" && echo "correct" || echo "incorrect" incorrect date: invalid date 'holaaa' $ date -d "<28 Mar 2017>" && echo "correct || echo "incorrect" correct

  • @Smoke the shape is without < > --> date -d"28 Mar 2017". But what exactly is your question?

  • I already set and took the biggest signal and the error that appears is this: . /Ex4.7.sh: line 5: $: command not found & #Xa;incorrect . /Ex4.7.sh: line 6: incorrect: command not found . /Ex4.7.sh: line 7: date:: command not found . /Ex4.7.sh: line 8: $: command not found & #Xa;incorrect . /Ex4.7.sh: line 9: Thu: command not found . /Ex4.7.sh: line 10: correct: command not found

  • Okay. Note my answer gave broad ideas on how to solve this, you have to put effort alone in the script. Also, better update your question with this code and specific questions about what is failing. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.