VboxManage list vms Linux Shell Script Always Outputs wrong result - linux

What am I doing wrong. I'm sure what I'm attempting to do should be clear to everyone.
#/usr/bin/env sh
echo -e "What VM?"
read vname
if [ '`VboxManage list vms | grep -c "$vname"`' != 0 ]; then
echo exists
fi
Anyone have suggestions. Pretty much it should tell me if the VM exist or not.

First your shebang is wrong.
Try this code
#!/usr/bin/env sh
echo -e "What VM?"
read -r vname
if [ "$(VboxManage list vms | grep -c "$vname")" != 0 ]; then
echo exists
fi

Related

How to read command output and use it silently on a bash script?

I'm new on bash. I want to write a script which will run iwconfig command the output should show me my wlan* mode is Managed or Monitor?
I had tried this like following but it is not working and runs the command output. I just need to echo which mode is it.
IWCONFIG_LOG="$(iwconfig)"
if [[ "$IWCONFIG_LOG" == *Mode:Managed* ]]; then
echo "Managed"
elif [[ "$IWCONFIG_LOG" == *Mode:Monitor* ]]; then
echo "Monitor Mode"
fi
Looks like you want to use Bash (not sh) in order to get this accomplish.
So, try this:
#!/bin/bash
IWCONFIG_LOG="$((iwconfig | grep Mode) 2> /dev/null)"
if [[ $IWCONFIG_LOG == *"Mode:Managed"* ]]; then
echo "Managed"
elif [[ $IWCONFIG_LOG == *"Mode:Monitor"* ]]; then
echo "Monitor Mode"
fi
Save it as a file and chmod 755 it to make it executable or execute it using "bash" instead "sh".
There are several modes, not only Managed or Monitor, so seems you only want to detect one of these two.
To see all available modes read man iwconfig
Since iwconfig writes also to standard error:
if [[ $(iwconfig 2>/dev/null | grep "Managed") ]]; then
echo "Managed"
else
echo "Monitor"
fi
or, as pointed out by #choroba in comments:
if iwconfig 2>/dev/null | grep -q Managed ; then
echo "Managed"
else
echo "Monitor"
fi

Is there a way to pipe user input within a bash script into the cat command and have it save at a destination of my choosing as a text file

Something similar to this maybe:
#! /bin/bash
echo What is your name?
read name | cat > ~/Documents/file.txt
if [[ $name==Bob ]]
echo something
fi
The command creates an empty file on manjaro mint.
Your problem is that read doesn't create any output.
And you have a syntax error further down the line, it would be a good idea to put your script(s) through shellcheck.
#! /bin/bash
echo What is your name?
read -r name
echo "$name" > ~/Documents/file.txt
if [[ "$name" == "Bob" ]]; then
echo something
fi

Problem with a script that checks for a distribution name and does something depending on the distribution value

I want to create a script that,depending on the distribution,runs a set of commands(in this example I want to check for Ubuntu and Centos). I've made a basic if-else statement in the script,but im new to bash and I can't understand why it doesn't work. Here's the code:
a= lsb_release -i | awk -F " " '{print $3}'
if [ "$a"=="Ubuntu" ]; then
echo "$a"
else
echo "Not ubuntu"
fi
Thanks in advance!

Bash script to create virtual clusters isn't working

I am trying to create a script to create virtual clusters on my virtual machine which is a CentOS 7 minimal.
I got a script named cluster
#!/bin/bash
function vc
{
echo
echo -n "Enter project name: "
read platform_name
echo
echo -n "web extension: "
read web_extension
echo
echo -e "The following website will be created"
echo -e "\e[32m Platform:\e[0m\t${platform_name}"
echo -e "\e[32m Extension:\e[0m\t${web_extension}"
echo -e "\e[32m Full URL:\e[0m\t http://www.${platform_name}.${web_extension}"
echo
echo -e "Do you wish to proceed? [Y/n]"
read -p "Are you sure? " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo
echo -e "\e[32m Creating platform \e[0m\t"
else
echo
echo -e "\e[32m Not creating platform \e[0m\t"
fi
}
if [ -n "$(type -t $FUNCTION_NAME)" ] && [ "$(type -t $FUNCTION_NAME)" =
function ];
then $FUNCTION_NAME $2; else help; fi
Then as far as I understood I just have to make it executable
chmod +x cluster
And after this I should make a syslink for it ln -s cluster /bin/cluster
And now I should normally be able to just typ cluster vc in the terminal and it should execute the script but it keeps giving me "command cluster not found"
Am I doing something obviously wrong? Or do I need to use another chmod on it so I can run this?
Symbolic link targets are resolved relative the the symlink location. In your case that means, if you run /bin/cluster it looks for a file named cluster (the target) in the /bin/directory. Either provide a relative path which points to your file or link to an absolute path: ln -s /path/to/cluster /bin/cluster.
Also make sure that the target location is readable and executable by whomever executes the symlink.

Linux: Reading the output of readlink /proc/pid/exe within a Bash Script

So I am writing a bash script which will run through all of the process ids in /proc/[pid] and read the executable that was used to run it.
From what I have had a looked at, the /proc filesystem contains the /proc/[pid]/exe symbolic link. Within the bash script I am trying work out how to read the value of "readlink /proc/[pid]/exe" to check if (deleted) or nothing is returned to find out whether the original executable exists on the disk or not.
Is there a way of doing this, so far I have?
#!/bin/bash
pid = "0"
while [ $pid -lt 32769 ]
do
if [-d /proc/$pid]; then
if [-f /proc/$pid/exe]; then
echo $pid
readlink /proc/$pid/exe
fi
fi
pid = $[$pid+1]
done
This fails to work and always returns nothing.I am trying to list all of the processes that no longer have their executables available on disk.
Will this work for you?
#!/bin/bash
for i in $(ls /proc | awk '/^[[:digit:]]+/{print $1}'); do
if [ -h /proc/$i/exe ]; then
echo -n "$i: "
if readlink /proc/$i/exe >/dev/null 2>&1 ; then
echo "executable exists"
else
echo "executable not found"
fi
fi
done
I've updated your script to make it work. Notice that -f checks whether a file name represents a regular file. I would return false for a symbolic link:
pid="0"
while [ $pid -lt 32769 ]
do
if [ -d /proc/$pid ]; then
if [ -h /proc/$pid/exe ]; then
echo $pid
readlink /proc/$pid/exe
fi
fi
pid=$[$pid+1]
done
you can read returned value after any command in shell by printing $? variable:
readlink
echo $?
if link is invalid, $? will be bigger than 0.
however if link exist and actual file is deleted, you can use something like:
ls `readlink somelink`
readlink -f `ls --dereference /proc/$pid/exe`

Resources