Swapped Mint14 for Mint16, issue running shell script

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
dwsa

Swapped Mint14 for Mint16, issue running shell script

Post by dwsa »

Hello, just this morning I installed Linux Mint 16 Cinnamon 32bit. I had been using mint 14 in the past and finally decided to update. I re-installed linux to my pc using the image found on this website. At which point, I noticed something. While I began to remake alot of my old scripts, I noticed one that now resulted in an error. The script I'm trying to run:

Code: Select all

#!/bin/bash
# init
echo & echo "(Website):" && read input
output=$(ping -c 1 "$input" 2>/dev/null)
if [ $? -eq 0 ]; then
     ip=$(printf '%s' "$output" | gawk -F'[()]' '/PING/{print $2}'  2>/dev/null)
     clear
     echo
     echo "$ip"; 
     echo -n $ip | xclip -selection c
     echo
     echo IP copied to clipboard.
     sleep 2
     exit
else
     clear
     echo "Host not found";
     sleep 1
     exit
fi  
When I installed Mint this morning, I made sure to update everything through the update manager. (levels 1 & 2)
I have not really changed the script, when I was using Linux Mint 14 (cinnamon) this very script worked without issue.

But now, when I open a terminal and type:

Code: Select all

sh ~/Desktop/shell.sh

it returns:

Code: Select all

: not found/Desktop/shell.sh: 3: /home/user/Desktop/shell.sh:  
(Website):  
The script says it can not be found, but continues. So, I type in "google.com" and press enter.
It returns:

Code: Select all

: bad variable name/shell.sh: 4: read: 
: not found/Desktop/shell.sh: 5: /home/user/Desktop/shell.sh: 
/home/user/Desktop/shell.sh: 24: /home/user/Desktop/shell.sh: Syntax error: word unexpected
Am I doing something wrong? Have I installed something improperly? Is this something with the shell itself?
I don't really understand where this error is coming from. Help is always appreciated.

Thanks in advance
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29507
Joined: Wed Jul 06, 2011 3:58 am

Re: Swapped Mint14 for Mint16, issue running shell script

Post by xenopeek »

Welcome to the Linux Mint forums :) Moved your topic here; the place to ask about shell scripts.

I'd urge you to do one more swap, namely to Linux Mint 17. Linux Mint 16 becomes obsolete shortly as it reaches end of support in July: http://www.linuxmint.com/download_all.php

As for the error you got, line 3 can't be correct--right? Shouldn't that be && instead of &
Image
dwsa

Re: Swapped Mint14 for Mint16, issue running shell script

Post by dwsa »

xenopeek wrote:Welcome to the Linux Mint forums :) Moved your topic here; the place to ask about shell scripts.

I'd urge you to do one more swap, namely to Linux Mint 17. Linux Mint 16 becomes obsolete shortly as it reaches end of support in July: http://www.linuxmint.com/download_all.php

As for the error you got, line 3 can't be correct--right? Shouldn't that be && instead of &
Hello, thanks for the welcome. I actually think I may have double posted this topic (sorry if I did).

The use of "&" is just a habbit I picked up with windows and serves no real purpose. I meant to delete that. (it's not in the script anymore)

I will take a look at Mint 17, thanks for the heads up!
User avatar
xenopeek
Level 25
Level 25
Posts: 29507
Joined: Wed Jul 06, 2011 3:58 am

Re: Swapped Mint14 for Mint16, issue running shell script

Post by xenopeek »

I'd write the

Code: Select all

echo & echo "(Website):" && read input
as

Code: Select all

echo
echo -n "(Website):"
read input
instead. -n suppresses the newline after echo.
Image
dwsa

Re: Swapped Mint14 for Mint16, issue running shell script

Post by dwsa »

Even with the changes, it still fails with the same error

Code: Select all

user@system ~ $ sh ~/Desktop/shell.sh
: not found/Desktop/shell.sh: 3: /home/user/Desktop/shell.sh: 
: not found/Desktop/shell.sh: 4: /home/user/Desktop/shell.sh: echo
(Website):
again, I type "google.com" and press enter:

Code: Select all

: bad variable name/shell.sh: 6: read: 
: not found/Desktop/shell.sh: 7: /home/user/Desktop/shell.sh: 
/home/user/Desktop/shell.sh: 26: /home/user/Desktop/shell.sh: Syntax error: word unexpected
I'm not sure why it keeps failing
WharfRat

Re: Swapped Mint14 for Mint16, issue running shell script

Post by WharfRat »

I tried running your original script in mint petra x86_64 and had no problems.

I unset $BASH in the environment, started it with ./shell.sh, bash -c ~/Desktop/shell.sh, sh ~/Desktop/shell.sh and it worked. So the errors you're getting is very odd.

The only suggestion I can make is try reinstalling bash.
dwsa

Re: Swapped Mint14 for Mint16, issue running shell script

Post by dwsa »

WharfRat wrote:I tried running your original script in mint petra x86_64 and had no problems.

I unset $BASH in the environment, started it with ./shell.sh, bash -c ~/Desktop/shell.sh, sh ~/Desktop/shell.sh and it worked. So the errors you're getting is very odd.

The only suggestion I can make is try reinstalling bash.
By bash, you mean the whole shell? How do I re-install the shell?
ps the output of "echo $BASH_VERSION" is:

Code: Select all

4.2.45(1)-release
WharfRat

Re: Swapped Mint14 for Mint16, issue running shell script

Post by WharfRat »

I'm at the same version.

Code: Select all

wharfrat@mint ~ $ bash --version
GNU bash, version 4.2.45(1)-release (x86_64-pc-linux-gnu)
bash is a package that can be installed so just reinstall it.

Code: Select all

sudo apt-get install --reinstall bash
Is there any difference if you

sudo bash -c ~/Desktop/shell.sh :?:

Also are you logged in as a guest user or is your user name user :?:
dwsa

Re: Swapped Mint14 for Mint16, issue running shell script

Post by dwsa »

WharfRat wrote:I'm at the same version.

Code: Select all

wharfrat@mint ~ $ bash --version
GNU bash, version 4.2.45(1)-release (x86_64-pc-linux-gnu)
bash is a package that can be installed so just reinstall it.

Code: Select all

sudo apt-get install --reinstall bash
Is there any difference if you

sudo bash -c ~/Desktop/shell.sh :?:

Also are you logged in as a guest user or is your user name user :?:
The user account is actually called user, even when I run with root the same error occurs.
I've even re-installed the shell itself, seems to have made no difference.

Does this mean I have a problem somewhere else? -Thanks in advance.
WharfRat

Re: Swapped Mint14 for Mint16, issue running shell script

Post by WharfRat »

I've never encountered an error like : not found/Desktop/shell.sh: 3: /home/user/Desktop/shell.sh:.

I see it starts complaining as soon as you start the script. Does any other script exhibit that problem.

I doubt your init scripts would complete successfully if it was happening with every script :?

Will this run without error :?:

Code: Select all

#!/bin/bash
#
echo -n "Memory " ;grep MemTotal: /proc/meminfo | awk '{printf( "%\047d\n", $2)}'
echo "Boot directory contents: " && ls -lh /boot | egrep 'kernel|initram|initrd|vmlinuz'
echo -n $(lscpu | grep MHz) ; [[ -e /usr/bin/cpufreq-set ]] && echo " CPU might be governed by cpufrequtils" || echo
[[ -e /usr/bin/cpufreq-set ]] && cpufreq-info -o || echo "Not governed by cpufrequtils"
echo -n "Sound card(s):"; echo -n $(grep -i codec /proc/asound/card*/codec\#* | cut -d: -f3);echo
exit 0
dwsa

Re: Swapped Mint14 for Mint16, issue running shell script

Post by dwsa »

WharfRat wrote:I've never encountered an error like : not found/Desktop/shell.sh: 3: /home/user/Desktop/shell.sh:.

I see it starts complaining as soon as you start the script. Does any other script exhibit that problem.

I doubt your init scripts would complete successfully if it was happening with every script :?

Will this run without error :?:

Code: Select all

#!/bin/bash
#
echo -n "Memory " ;grep MemTotal: /proc/meminfo | awk '{printf( "%\047d\n", $2)}'
echo "Boot directory contents: " && ls -lh /boot | egrep 'kernel|initram|initrd|vmlinuz'
echo -n $(lscpu | grep MHz) ; [[ -e /usr/bin/cpufreq-set ]] && echo " CPU might be governed by cpufrequtils" || echo
[[ -e /usr/bin/cpufreq-set ]] && cpufreq-info -o || echo "Not governed by cpufrequtils"
echo -n "Sound card(s):"; echo -n $(grep -i codec /proc/asound/card*/codec\#* | cut -d: -f3);echo
exit 0
As of now, this is the only script resulting in an error that I could not explain.
The output of above is

Code: Select all

user@system ~ $ #!/bin/bash
user@system ~ $ #
user@system ~ $ echo -n "Memory " ;grep MemTotal: /proc/meminfo | awk '{printf( "%\047d\n", $2)}'
Memory 3,721,512
user@system ~ $ echo "Boot directory contents: " && ls -lh /boot | egrep 'kernel|initram|initrd|vmlinuz'
Boot directory contents: 
-rw-r--r-- 1 root root  25M Jun 23 10:11 initrd.img-3.11.0-12-generic
-rw-r--r-- 1 root root 5.4M Nov 26  2013 vmlinuz-3.11.0-12-generic
user@system ~ $ echo -n $(lscpu | grep MHz) ; [[ -e /usr/bin/cpufreq-set ]] && echo " CPU might be governed by cpufrequtils" || echo
CPU MHz: 1320.000
user@system ~ $ [[ -e /usr/bin/cpufreq-set ]] && cpufreq-info -o || echo "Not governed by cpufrequtils"
Not governed by cpufrequtils
user@system ~ $ echo -n "Sound card(s):"; echo -n $(grep -i codec /proc/asound/card*/codec\#* | cut -d: -f3);echo
Sound card(s):
Last edited by dwsa on Tue Jun 24, 2014 1:27 am, edited 1 time in total.
User avatar
xenopeek
Level 25
Level 25
Posts: 29507
Joined: Wed Jul 06, 2011 3:58 am

Re: Swapped Mint14 for Mint16, issue running shell script

Post by xenopeek »

He meant putting that in a file, making it executable, and running it. Not pasting it on the terminal :|
Image
dwsa

Re: Swapped Mint14 for Mint16, issue running shell script

Post by dwsa »

xenopeek wrote:He meant putting that in a file, making it executable, and running it. Not pasting it on the terminal :|
thats exactly what I did. .?

I took the code he gave me, put it in a shell.sh, and it returned the errors above. What did I do wrong? :| Now I'm confused..
WharfRat

Re: Swapped Mint14 for Mint16, issue running shell script

Post by WharfRat »

Did you save what I gave you to a filename shell.sh :?:

Can you cat and ls -lh the file you created :?:
Locked

Return to “Scripts & Bash”