0

I'm working on a script using bash to determine the os being used and print to the screen the version.

The only line giving me issues is this one, could you please help me?:

  • 1
    Please state the exact error message. *"I was told it has a syntax issue..."* is not a good problem statement. Also see [Detect the OS from a Bash script](https://stackoverflow.com/q/394230/608639) – jww Nov 15 '17 at 15:11

4 Answers4

2

An alternative is to use

lsb_release -d 

for getting the distribution name, or

lsb_release -a

to get all info about the distro.

More info about this linux standard base utility.

oliv
  • 12,690
  • 25
  • 45
1

You can't have spaces between the two sides of the = sign, and you need to execute the right side as a subcommand (using $() syntax).

current_os=$(cat /etc/*-release | grep CentOS)

With spaces like current_os = ... bash will interpret it as you trying to run a command called current_os.

wkl
  • 77,184
  • 16
  • 165
  • 176
  • Great! I tried this and it printed the correct os name on centos. When I run this command on Ubuntu, it doesn't work. –  Nov 15 '17 at 15:13
  • @think - you'll need to post the contents of your script for help with that. – wkl Nov 15 '17 at 15:14
  • @think: you probably have windows line endings in your script. `dos2unix` can fix that. – Mat Nov 15 '17 at 15:15
  • @think - Mat is likely correct and you may have line-endings that are causing problems. Run `dos2unix os-script.txt` and try again. – wkl Nov 15 '17 at 15:23
  • Okay, I believe my ubuntu VM needs an internet connection to run dos2unix, which it doesn't right now. I'll try. –  Nov 15 '17 at 15:26
  • @think `sed -i 's/.$//' os-script.txt` should do it as well, and your system should already have `sed` installed. – wkl Nov 15 '17 at 15:29
  • @think are you in the same directory as `os-script.txt`? Otherwise provide the full path to it. – wkl Nov 15 '17 at 15:35
  • I went to the correct directory before typing in sed. I think it took it, but I'm still getting syntax errors for this: function CurrentOs(){ –  Nov 15 '17 at 15:38
  • @think ah I see it now - `function name()` is not valid bash syntax for declaring functions. Either use the `function name { }` format, or `name () { }` format. – wkl Nov 15 '17 at 15:50
  • Great, it fixed line 1 issue. I changed to just this: CurrentOs() {} There is an issue somewhere with my if, elif, or else statements, because it will print that the os is Centos on Ubuntu. –  Nov 15 '17 at 15:59
  • Do you see anything wrong with my if statement? if(CurrentOs=$"CentOS Linux"); then {} –  Nov 15 '17 at 16:02
  • @think shouldn't it be `if [[ $CurrentOS = "CentOS Linux ]]; then ...`? Also, since it seems you have other problems now, I'd recommend opening a separate question and including the contents of your entire script in the question and asking what's causing your syntax issues. It would be a lot easier for you to get help rather than ask in comments. It would also let people see your entire script and possibly point out extra problems. – wkl Nov 15 '17 at 16:10
0
source /etc/os-release
current_os=$PRETTY_NAME

or if you don't want to pull everything in

current_os=$(source /etc/os-release && echo $PRETTY_NAME)
user3100381
  • 464
  • 2
  • 5
-1

Put the commands in backticks (``) and the statement will assign the output of the command.

current_os=`cat /etc/*-release | grep CentOS`
René Pijl
  • 4,310
  • 1
  • 19
  • 25