Shell Script Filter and Execution

Asked

Viewed 541 times

-1

I have a problem in Shell Script, where:

  • You need to report a file;
  • Check if it’s a file .C, .JAVA or neither of;
  • If you are either, compile as chosen and run;

(I don’t know much about Shell)

#!/bin/sh

dialog --backtitle "Código FOnte" --title "Menu" --menu "Selecione " 0 0 6 \
  1 "Exibir status das utilizações das partições" \
  2 "Relação de usuários logados " \
  3 "Informe um arquivo e receba sua informação em bytes " \
  4 "Passe um programa em C ou java e execute " 2>/tmp/menuitem.$$
  menuitem=`cat /tmp/menuitem.$$`

  opt=$?

 case $menuitem in
  1) df -h > /tmp/item.1 && dialog --textbox /tmp/item.1 20 80  ;;
  2) who > /tmp/item.2 && dialog --textbox /tmp/item.2 20 80  ;;
  3) dialog --inputbox 'Digite caminho e o arquivo :' 0 0  2>/tmp/nome.txt
     caminho=$( cat /tmp/nome.txt )
     arquivo=$( ls -lh $caminho | awk '{print $9 "------------------------------------->" $5}') ;;
  4) ;;
esac
  • What do you want? create a Shell Script file? vc has no example code?

  • Yes, I want to create a file in Shell Script

2 answers

0

You can check the file extension based on its last characters, as in the example below where I check the last 4:

#!/bin/bash


echo "qual o arquivo?"
read arquivo

if [ ${arquivo: -4} == ".txt" ]; then
    echo "arquivo tipo txt"
elif [ ${arquivo: -4} == ".doc" ]; then
    echo "arquivo tipo doc"
else
    echo "arquivo invalido"
fi
  • He informs : Bad substitution

  • Where exactly did he file this error? Paste your code adapted here for analysis, please.

0

I follow the previous answer, just add a few more things:

#!/bin/bash

echo "qual o arquivo?"
read arquivo

# testa se é um arquivo regular    
if [ ! -f "${arquivo}" ] ; then
   echo "${arquivo}" não encontrado
   exit 1
fi


# método1: obtenção da extensão diretamente na atribuição de variável
extensao=${arquivo##*.}

# se não funcionar no teu shell, utilize este outro código
# método2: obtenção da extensão com ajuda do awk
# extensao=`echo ${arquivo} | awk -F "." '{print $NF;}'`

# conversão para minuscula
extensao=`echo "${extensao}" | tr 'A-Z' 'a-z'`

# tratamento do arquivo
if [ "${extensao}" == "c" ]; then
    echo "arquivo C"
    # rode um teste de compilação C
elif [ "${extensao}" == "java" ]; then
    echo "arquivo java"
    # rode um teste de compilação java
else
    echo "arquivo invalido"
fi

Note that it is interesting to normalize the case of extensions so as not to have to test upper and lower case. What may have given the error is that commands like this ${arquivo: -4} not supported in the shell /bin/sh, there is another option to do in awk (commented command).

The command dialog (or Whiptail in debian) is interesting to generate dialogues (menus, questions type yes-no, etc) but I also find unnecessary for the problem.

  • That’s exactly what I was needing @gwarah!! But I’m having a hard time running the program, where in case it’s a code on C, it needs to compile and run it. It should look like? Example: if [ "${extensao}" == "c" ]; then
 gcc $arquivo -o prog #Compilar
 ./prog #Executar
 #Mostrar

  • C is not my thing @Luiz, it’s been a while since I program, but I think this is the syntax really. Isn’t it something in the file contents that is breaking? Post error output.

  • In fact it shows no exit, it just stands still without performing any operation!

  • Before executing the gcc, puts a echo sinal 1 and after the gcc one echo sinal 2 and after . /Prog, echo sinal 3. I made a change to the program to test whether the ${arquivo} exists in the same program directory. Make these changes and post the result.

Browser other questions tagged

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