Requesting coding assistance for a linux script

For discussions about programming, and for programming questions and advice


Moderator: Forum moderators

Post Reply
Clarity
Posts: 3369
Joined: Fri Jul 24, 2020 10:59 pm
Has thanked: 1387 times
Been thanked: 446 times

Requesting coding assistance for a linux script

Post by Clarity »

I am NOT a programmer. But, I need some help to complete a manual that would be useful to this forum's distro use.

I have made a script attempt to get started, here, but, have problem with the current attempt.

Can anyone help by steering me to a solution in the current script attempt?

Thanks in advance for any help from forum members.

Last edited by Clarity on Thu Jan 12, 2023 9:33 pm, edited 1 time in total.
icake
Posts: 418
Joined: Mon Jul 27, 2020 8:07 am
Been thanked: 44 times

Re: Requesting coding assistance for a linus script

Post by icake »

There are many ways to do it, here is one:
#!/bin/bash

echo 'Running a test named/n' $0

egrep --color=always -i 'vmx|svm' /proc/cpuinfo > nul # Is PC VM capable?
if test $? -eq 0
then echo "This PC has VM capability"
exit
else echo "This PC does not have VM capability"
sleep 2
fi
exit

icake

Burunduk
Posts: 245
Joined: Thu Jun 16, 2022 6:16 pm
Has thanked: 6 times
Been thanked: 123 times

Re: Requesting coding assistance for a linus script

Post by Burunduk »

Clarity wrote: Wed Jan 11, 2023 10:19 am

Code: Select all

#!/bin/bash

echo 'Running a test named/n' $0

egrep --color=always -i 'vmx|svm' /proc/cpuinfo >  nul # Is PC VM capable?
if [ $? eq 0]
	then echo "This PC has VM capability"
		exit
else echo "This PC does not have VM capability"
sleep 2
exit

This should do what you want:

Code: Select all

if grep -Eiq 'vmx|svm' /proc/cpuinfo; then
    echo "This PC has VM capability"
else
    echo "This PC does not have VM capability"
fi

Notes:

[ $? eq 0] should be a dash before eq and a space before ]: [ $? -eq 0 ]

> nul redirects output to a file named "nul". To discard output: >/dev/null
-q flag makes grep output nothing and only return a code.

Any command(s) can be used after if, not only [ ... ] or test.

Clarity
Posts: 3369
Joined: Fri Jul 24, 2020 10:59 pm
Has thanked: 1387 times
Been thanked: 446 times

Re: Requesting coding assistance for a linus script

Post by Clarity »

@icake and @Burunduk I have expanded the changes from your guidance to address the flowchart on that thread.

My new code taking your suggestion in an attempt to complete the script is here. It is failing on line 12 of that script. See here.

Any help is muchly appreciated.

Burunduk
Posts: 245
Joined: Thu Jun 16, 2022 6:16 pm
Has thanked: 6 times
Been thanked: 123 times

Re: Requesting coding assistance for a linus script

Post by Burunduk »

Clarity wrote: Thu Jan 12, 2023 12:15 pm

Code: Select all

#!/bin/bash
# Credit to this script goes to the combined efforts 
# of Puppy Linux forum members
# 	especially members 'icake' and 'Burunduk'

echo 'Running a test named/n' $0

if grep -Eiq 'vmx|svm' /proc/cpuinfo; then
    echo "This PC has VM capability"
    if lsmod | egrep --color=always kvm # is virtualization loaded?; then 
		echo "This PC has KVM loaded"
		exit
	else
		echo "KVM is being loaded"
		if grep -Eiq 'vmx' /proc/cpuinfo; then
			modprobe kvm-intel # vmx flag is for Intel CPUs 
			exit
		else 
		modprobe kvm-amd # svm flag is for AMD CPUs 
		exit
	fi
fi
else
    echo "This PC does not have VM capability"
    exit
fi

.
...

Code: Select all

line 13: syntax error near unexpected token `else'
line 13: `	else'

First of all you've commented out ; then on line 10

This is a possible variant:

Code: Select all

echo $'Running a test named\n'"$0"
if grep -Eiq 'vmx|svm' /proc/cpuinfo; then
    echo "This PC has VM capability"
    if lsmod | grep -iq kvm; then # is virtualization loaded?
        echo "This PC has KVM loaded"
    else
        echo "KVM is being loaded"
        if grep -iq 'vmx' /proc/cpuinfo; then
            modprobe kvm-intel # vmx flag is for Intel CPUs 
        else 
            modprobe kvm-amd # svm flag is for AMD CPUs 
        fi
    fi
else
    echo "This PC does not have VM capability"
fi

Didn't test it though. I know very little about virtualization.

---

Edit: Just as an example, a less nested variant without if:
(Please don't use it if you don't fully understand how it works. Unlike grep -i its tests are case sensitive. Also it may not work with older bash versions.)

Code: Select all

#!/bin/bash
echo $'Running a test named\n'"$0"

c=none
[[ `</proc/cpuinfo` =~ vmx|svm ]] && c=$BASH_REMATCH && [[ `lsmod` =~ kvm ]] && c=loaded
# is virtualization loaded? and is kvm string unique enough?

case $c in

    none)
        echo "This PC does not have VM capability"
        ;;
    *)
        echo "This PC has VM capability"
        ;;&
    loaded)
        echo "This PC has KVM loaded"
        ;;
    *)
        echo "KVM is being loaded"
        ;;&
    vmx)
        modprobe kvm-intel # vmx flag is for Intel CPUs
        ;;
    svm)
        modprobe kvm-amd # svm flag is for AMD CPUs
        ;;
esac
Clarity
Posts: 3369
Joined: Fri Jul 24, 2020 10:59 pm
Has thanked: 1387 times
Been thanked: 446 times

Re: Requesting coding assistance for a linus script

Post by Clarity »

Burunduk wrote: Thu Jan 12, 2023 2:08 pm

This is a possible variant:

Code: Select all

echo $'Running a test named\n'"$0"
...
fi

Thanks. This suggestion is a good one as you show that the multiple 'exit' in the script is unnecessary. And you show ny error use of 1st echo command in the script ... yes I was scratching my head on the new line attempt.

I tested the other version you've posted for review and it 'mirrors' the script you helped to correct. Nice!

I also ran some timings to compare:

  • The code we used

    Code: Select all

    This PC has VM capability
    This PC has KVM loaded
    
    real	0m0.023s
    user	0m0.006s
    sys	0m0.007s
  • The other version you show using 'case' instead

    Code: Select all

    This PC has VM capability
    This PC has KVM loaded
    
    real	0m0.022s
    user	0m0.003s
    sys	0m0.008s

Your 'case' version appears to be more efficient. I will have to study your script more to have a understanding.

Last edited by Clarity on Thu Jan 12, 2023 9:24 pm, edited 2 times in total.
Burunduk
Posts: 245
Joined: Thu Jun 16, 2022 6:16 pm
Has thanked: 6 times
Been thanked: 123 times

Re: Requesting coding assistance for a linux script

Post by Burunduk »

The time difference is insignificant here. It may accumulate in loops though. Calling an external command (grep) takes more time
than using a bash builtin ([[ ... ]]).

This will be a bit faster too: if [[ `</proc/cpuinfo` =~ vmx|svm ]]; then

---

Notes:

`</root/a_file` is replaced with a content of a file. It should be quoted outside of [[ ... ]]: "$(</root/a_file)".

=~ pattern searches for a regex pattern (unquoted) or a string (quoted).

$BASH_REMATCH contains a matched part of a string.

... && .. && .. works here but don't do that

;;& in the case statement means "continue as if the pattern list had not matched".

Print 2 lines:

Code: Select all

printf "line 1\nline 2\n"

printf 'line 1\nline 2\n'

echo "line 1"
echo "line 2"

echo "line 1
line 2"

echo $'line 1\nline 2'

echo -e "line 1\nline 2"
Post Reply

Return to “Programming”