0

I've been learning shell scripting since yesterday. I want to make a script that returns the minimum number from all the numbers in a text file. This is what I have so far:

#!/bin/bash
file="example.txt"
min=cat $file|head -1
for i in $(cat $file); do
  if [[ $min -gt $i ]]; then
    min=$i
  fi
done
echo $min

I keep getting an error in line 3 that says "example.txt: command not found".

Juan Diego Godoy Robles
  • 14,447
  • 2
  • 38
  • 52
Zantorym
  • 99
  • 1
  • 7
  • This should get you started: https://stackoverflow.com/questions/9449778/what-is-the-benefit-of-using-instead-of-backticks-in-shell-scripts – fvu Oct 05 '17 at 06:39
  • Chuckling... Stick with it, you will make friends with your shell soon enough. You will want to bookmark [**Bash Guide**](http://mywiki.wooledge.org/BashGuide), [**Bash FAQ**](http://mywiki.wooledge.org/BashFAQ) and [**Bash Pitfalls**](http://mywiki.wooledge.org/BashPitfalls). A wealth of good information can be found at each. – David C. Rankin Oct 05 '17 at 06:47

2 Answers2

0

You need to use a command substitution:

min=$(cat $file|head -1)

Besides that, note that the cat command is useless here since head accepts a filename. It should be:

min=$(head -1 "$file")
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
Juan Diego Godoy Robles
  • 14,447
  • 2
  • 38
  • 52
0

add it inside backticks.

 min=`cat $file|head -1`
abhishek phukan
  • 751
  • 1
  • 5
  • 16