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
  • 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 Commented Nov 15, 2017 at 15:11

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

12 Comments

Great! I tried this and it printed the correct os name on centos. When I run this command on Ubuntu, it doesn't work.
@think - you'll need to post the contents of your script for help with that.
@think: you probably have windows line endings in your script. dos2unix can fix that.
@think - Mat is likely correct and you may have line-endings that are causing problems. Run dos2unix os-script.txt and try again.
Okay, I believe my ubuntu VM needs an internet connection to run dos2unix, which it doesn't right now. I'll try.
|
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)

Comments

-1

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

current_os=`cat /etc/*-release | grep CentOS`

4 Comments

Syntax still invalid, and backticks are not recommended, $() is better
$() will give you exactly the same result in this case, so it's not better, it's just another solution.
It's better advice in general.
Backticks are considered obsolete, as they are harder to read and much harder to nest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.