LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-25-2018, 07:40 AM   #1
theCapitain
Member
 
Registered: Jul 2010
Location: Northern Italy
Distribution: Slackware, Slax
Posts: 59

Rep: Reputation: 2
Script command vs. terminal input


Hello,
probably a really basic question, but there it is: does somebody want to tell me why this simple script doesn't work

Code:
#!/bin/bash

unset PATH  

BACKUP_DEVICE="/dev/sdg1"
MOUNTING_POINT="/root/snapshot"

mount -o rw $BACKUP_DEVICE $MOUNTING_POINT
Giving the following error message:

Code:
./test.sh: line 8: mount: No such file or directory
While the exact same commands pasted on the terminal work perfectly?
I have similar troubles with other commands, not only “mount”, so I think I’m doing something wrong.

Thanks for helping
 
Old 11-25-2018, 08:23 AM   #2
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,706
Blog Entries: 19

Rep: Reputation: 4506Reputation: 4506Reputation: 4506Reputation: 4506Reputation: 4506Reputation: 4506Reputation: 4506Reputation: 4506Reputation: 4506Reputation: 4506Reputation: 4506
If you unset your PATH, bash does not know where to look for executables. So it concludes that they don't exist.

I find no difference when I unset my PATH in a terminal and then call mount.
 
Old 11-25-2018, 08:34 AM   #3
lougavulin
Member
 
Registered: Jul 2018
Distribution: Slackware,x86_64,current
Posts: 280

Rep: Reputation: 100Reputation: 100
Did you write the script yourself ?
What is/was the purpose to unset PATH ? I'm curious to know how or why you came to do that.
 
Old 11-26-2018, 07:25 AM   #4
theCapitain
Member
 
Registered: Jul 2010
Location: Northern Italy
Distribution: Slackware, Slax
Posts: 59

Original Poster
Rep: Reputation: 2
Hi hazel,
thanks for your hint: actually I've taken the script from the web, but I've made a modification. The original was "slightly" different:
Code:
MOUNT=/bin/mount
...
MOUNT -o rw $BACKUP_DEVICE $MOUNTING_POINT
well, I didn't see a point in doing that chain

@lougavulin: the purpose to unset PATH, based on the source script comments, is to avoid accidental use of $PATH. Do you think it is unnecessary?
 
Old 11-26-2018, 07:37 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,129

Rep: Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374
unset PATH means you remove that variable. bash usually uses it, so removing it is not a good idea.
MOUNT should be used as any other variable: $MOUNT.
 
Old 11-26-2018, 07:38 AM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
you're the programmer, I do not know how PATH would get accidentally used, unless the script was written
Code:
mount -o rw $PATH $DESTINATION
source -> destination

Last edited by BW-userx; 11-26-2018 at 08:02 AM.
 
Old 11-26-2018, 09:01 AM   #7
nodir
Member
 
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
Quote:
Originally Posted by theCapitain View Post
Hi hazel,
thanks for your hint: actually I've taken the script from the web, but I've made a modification. The original was "slightly" different:
Code:
MOUNT=/bin/mount
...
MOUNT -o rw $BACKUP_DEVICE $MOUNTING_POINT
well, I didn't see a point in doing that chain

@lougavulin: the purpose to unset PATH, based on the source script comments, is to avoid accidental use of $PATH. Do you think it is unnecessary?
1) "Variables hold data. Functions hold code. Don't put code inside variables! " from here:
http://mywiki.wooledge.org/BashFAQ/050
(i for one am not that sure in this case, just saying)
2) don't user upper case variables
3) quote *every* substitution
4) #bash IRC bot "Google is NOT a preferred source for learning bash, because almost all the "tutorials" and scripts out there are JUNK." (the reasoning for that harsh comment should be clear from that very short example you found via google).

Last edited by nodir; 11-26-2018 at 09:03 AM.
 
Old 11-26-2018, 09:05 AM   #8
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,767

Rep: Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225
Quote:
Originally Posted by nodir View Post
1) "Variables hold data. Functions hold code. Don't put code inside variables! " from here:
http://mywiki.wooledge.org/BashFAQ/050
(i for one am not that sure in this case, just saying)
2) don't user upper case variables
3) quote *every* substitution
4) #bash IRC bot "Google is NOT a preferred source for learning bash, because almost all the "tutorials" and scripts out there are JUNK." (the reasoning for that harsh comment should be clear from that very short example you found via google).
Disagree strongly with #2! In a case-sensitive environment, upper-case variables are recommended.
 
Old 11-26-2018, 09:07 AM   #9
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,767

Rep: Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225
Quote:
Originally Posted by theCapitain View Post
the purpose to unset PATH, based on the source script comments, is to avoid accidental use of $PATH. Do you think it is unnecessary?
Not only unnecessary, but probably unwise, IMO.
Note (again), that unsetting $PATH has caused the mount command to fail.
 
Old 11-26-2018, 09:23 AM   #10
nodir
Member
 
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
Quote:
Originally Posted by scasey View Post
Disagree strongly with #2! In a case-sensitive environment, upper-case variables are recommended.
recommended by whom? sure not by #bash
Quote:
By convention, environment variables (PATH, EDITOR, SHELL, ...) and internal shell variables (BASH_VERSION, RANDOM, ...) are fully capitalized. All other variable names should be lowercase. Since variable names are case-sensitive, this convention avoids accidentally overriding environmental and internal variables.

Last edited by nodir; 11-26-2018 at 09:26 AM.
 
Old 11-26-2018, 11:18 AM   #11
theCapitain
Member
 
Registered: Jul 2010
Location: Northern Italy
Distribution: Slackware, Slax
Posts: 59

Original Poster
Rep: Reputation: 2
Quote:
Originally Posted by scasey View Post
Note (again), that unsetting $PATH has caused the mount command to fail.
Sure, it's crystal clear now, I got this all wrong. But my script now works great.
Thank you all.
 
Old 11-26-2018, 03:51 PM   #12
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Rep: Reputation: 145Reputation: 145
Quote:
Originally Posted by theCapitain View Post
But my script now works great.
Excellent!

Quote:
1) "Variables hold data. Functions hold code. Don't put code inside variables! " from here:
http://mywiki.wooledge.org/BashFAQ/050
(i for one am not that sure in this case, just saying)
2) don't user upper case variables
3) quote *every* substitution
4) #bash IRC bot "Google is NOT a preferred source for learning bash, because almost all the "tutorials" and scripts out there are JUNK."
I recommend ignoring this advice.
 
1 members found this post helpful.
Old 11-26-2018, 04:17 PM   #13
nodir
Member
 
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
"this advise" you recommend to ignore is from mywki.wooledge.org, #bash irc channel and wiki.bash-hackers.org.
I gave the links and the quotes for the reasoning, instead of simply a "recommendation". User can decide on their own if it makes sense or not.

Last edited by nodir; 11-26-2018 at 04:29 PM.
 
1 members found this post helpful.
Old 11-26-2018, 06:17 PM   #14
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Rep: Reputation: 145Reputation: 145
Quote:
Originally Posted by nodir View Post
3) quote *every* substitution
Let me give just one reason why just one of these is bad. Do you think this is likely to do what a user intends?
Code:
LIST="file1 file2 file3"
for f in "$LIST"; do     # Quote *every* substitution!
    process "$f"
done
 
1 members found this post helpful.
Old 11-27-2018, 12:57 AM   #15
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,129

Rep: Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374Reputation: 7374
Quote:
Originally Posted by KenJackson View Post
Let me give just one reason why just one of these is bad. Do you think this is likely to do what a user intends?
Code:
LIST="file1 file2 file3"
for f in "$LIST"; do     # Quote *every* substitution!
    process "$f"
done
I'm sorry, but this is not a really good example. It can hardly handle filenames containing spaces.
Code:
LIST=( "file1" "file2" "file3" )
for f in "${LIST[@]}"; do     # Quote *every* substitution!
    process "$f"
done
is the correct syntax.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [SOLVED] bash script: can echo command, command works if I type, but command doesn't work in script ... why? hopeless_n00b Linux - Newbie 10 07-12-2018 05:57 AM
Bash Script For Reading User Input then Compressing That Input to Tar file braveranger Linux - Newbie 13 11-15-2017 09:36 AM
[SOLVED] Write a shell script that receives a word, an input file and an output file. The scripts copies all the lines in the input file that contain mandy2112 Linux - Newbie 3 08-18-2016 10:11 AM
How to set the behavior when input command exceeds terminal max columns yech Linux - General 3 03-12-2013 06:22 PM
Repeated "input: AT Translated Set 2 keyboard as /class/input/input" messages AcerKev Mandriva 2 09-16-2007 08:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration