summarylogtreecommitdiffstats
path: root/dist_detect_main
blob: b3549aee9ed11715f3171abc2287f01dcc6b9acd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash 

#Author : Amirreza Firoozi
#License : GPL3
#version : 1.8

#This is a simple script that find distro name and echo (return) it.
#Its useful if you want to know what distro is your own script running on.
previous_way(){
#This is the way we used in previous_way version(s)
distros=( "Arch" "Ubuntu" "Debian" "Suse" )
dId=0
while [ "1" == "1" ];do
    grep -i "${distros[dId]}" /etc/issue  1>/dev/null 2>&1
    exstatus=$?
     if [ "$exstatus" == "0" ];then #if grep found dist_name successfuly , we save dist_name in distro
              distro=${distros[dId]}
        
        break 
     fi

     if [ "$dId" -gt  "${#distros[@]}" ];then #if the number of loop is greater than number of distros we will break the loop

        break
     fi 
     ((dId++))
done
}
#This is the current_way .
#As you can see its more powerful and reliable and need less time :D 
current_way(){

     distro=$(egrep "^NAME" /etc/os-release | sed s/"NAME="// | sed s/\"//g)

}
#In previous version we used if to make argument support
#in this version we use the correct way of it (getopts)
current_way 

if [ "$#" == "0" ];then 
    echo "$distro"
 exit 0
fi 


while getopts ":o:p" OPTIONS; do
    case $OPTIONS in
        
     o) #redirects output to a file
        while [ "$#" -gt "1" ];do
            FILE=$2
           echo "$distro" > $FILE

          shift #Move on to next input file
        done

        ;;

     p) #use previous_way 
        previous_way

        if [ "$#" == "1" ];then 
                echo "$distro"
                     exit 0
        fi 
        
        ;;

     *) #unrecognized option 
        
        echo -e " Invalid Option \n see man dist_detect if you need help "
    exit 1

        ;;
    esac
done
shift $((OPTIND-1)) #This tells getopts to move on to the next argument