Never confuse education with intelligence, you can have a PhD and still be an idiot.
- Richard Feynman -



Bash FAQ

From Juneday education
(Redirected from FAQ)
Jump to: navigation, search

This FAQ primarily deals with problems when running bash and bash scripts. It is useful to have this page regardless of what book or course you are taking/reading in this Wiki, since we tend to use bash everywhere.

Bash / terminal

"permission denied"

Q: I get "permission denied" when I try to execute a script or one of your commands/programs. What am I doing wrong?

A: Most likely you have downloaded a script from internet (e g github.com). In order to execute a script you must make sure the permissions of the script/program allow this. To do this you type

chmod u+x scriptname.sh
(where scriptname.sh should be the name of the script you want to change the permissions on). For more information check out the Exercises on Scripts Introduction


Note: If you would like to change the permissions of many files at the same time you can type chmod u+x *.sh which changes the permission on all files with the suffix sh in the current directory.

You still need to run the script using a relative path to the script, since you probably downloaded the script to a directory which is not in your PATH. If you are in the same directory as the script, you need to run it using:

$ ./scriptname.sh

As you know from the bash chapters, the ./ part means "current directory slash" which is a relative path to a file in the current directory. The dot always signifies "current directory" when used as part of a path. The slash is the separator between directories or between directories and files in any UNIX-like environment (like bash).

Check out the following page to learn more about permissions: Download_and_execute_scripts.

Read more:

xxx: command not found

Q: I get "xxx: Command not found" when i try to run the command xxx in the command line (e.g. javac, atom, notepad++). Why can't I run the command? I have just installed the program!

A: When you type the name of a command or program in a terminal running Bash, you are talking directly to the Bash command interpreter. It will interpret your command (e.g. atom ) as if you want to run a program. But Bash has no idea where (or even if) you have installed the atom executable (the file with the actual atom program in this case). As a piece of help, Bash has a list of paths with folders to investigate when looking for a program to run. This list is stored in $PATH in the Bash environment.

If you are running Windows, you must add the path to programs you install and wish to be able to run from the command line in the windows %path% variable. You can access this variable using Control Panel and System Environment Variables. Bash (started via Cygwin) will read this variable when you start a new terminal and add it to its own $PATH variable.

If you are running a UNIX-like system like GNU/Linux or Mac OS X, you must add the path to the program to your user's bash configuration file (e.g. ~/.bashrc) to the $PATH variable. Example of adding a path to ~/.bashrc:

# A line in the .bashrc file below:
export PATH=$PATH:/path/to/installation/directory

Read more:

My tool xxx is not working on my Mac as in your videos

Some of the commands in Mac come from BSD and not from GNU. If you get a different result from a program, e g stat, you can do the following to check if you're using a GNU or BSD version:

$ man stat

which should give you something like this:

STAT(1)                   BSD General Commands Manual                  STAT(1)

NAME
     readlink, stat -- display file status

SYNOPSIS
     stat [-FLnq] [-f format | -l | -r | -s | -x] [-t timefmt] [file ...]
     readlink [-n] [file ...]

DESCRIPTION
     The stat utility displays information about the file pointed to by file.
     Read, write or execute permissions of the named file are not required,
     but all directories listed in the path name leading to the file must be
     searchable.  If no argument is given, stat displays information about the
     file descriptor for standard input.

     When invoked as readlink, only the target of the symbolic link is
     printed.  If the given argument is not a symbolic link, readlink will
     print nothing and exit with an error.

     The information displayed is obtained by calling lstat(2) with the given
     argument and evaluating the returned structure.

You can see from the first lines that this is a BSD version. If you want to use the GNU versions of the tool you can use Homebrew or MacPorts.

Comparing this to a GNU version of stat:

$ man stat

which should give you something like this:

STAT(1)                          User Commands                         STAT(1)

NAME
       stat - display file or file system status

SYNOPSIS
       stat [OPTION]... FILE...

DESCRIPTION
       Display file or file system status.

       Mandatory  arguments  to  long  options are mandatory for short options
       too.

       -L, --dereference
              follow links

       -f, --file-system
              display file system status instead of file status

       -c  --format=FORMAT
              use the specified FORMAT instead of the default; output  a  new‐
              line after each use of FORMAT

curl downloads a JAR-file but it is saved as an HTML page

In some versions of curl (typically on older versions of MacOS), curl downloads a file (e.g. a zip file or a jar file) but treats it as if it were an HTML page (which is wrong). In such cases, you can try the following flags to curl:

$ curl -k -LJO http://some-link-to/some-file.jar

Refer to the curl manual on your system for an explanation. In short:

  • -L tells curl to follow moved files (caring about the Location: header in the HTTP response)
  • -J tells curl to use the Content-Disposition file name sent by the server
  • -O tells curl to use the file name as it is given as part of the URL (the way wget does it)
  • -k tells curl to accept to download over HTTPS without checking the certificates as explained here

Certicate errors with wget

Error (swedish):

$ wget https://raw.githubusercontent.com/progund/tig015-weekly/master/weekly02/get_latest_jar.sh
--2017-09-08 11:01:11--  https://raw.githubusercontent.com/progund/tig015-weekly/master/weekly02/get_latest_jar.sh
Slår upp raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.84.133
Ansluter till raw.githubusercontent.com (raw.githubusercontent.com)|151.101.84.133|:443... ansluten.
FEL: kan inte validera certifikatet för raw.githubusercontent.com, utfärdat av "CN=DigiCert SHA2 High Assurance Server CA,OU=www.digicert.com,O=DigiCert Inc,C=US":
  Kunde inte lokalt verifiera utfärdarens auktoritet.
För att ansluta till raw.githubusercontent.com på osäkert sätt, använd "--no-check-certificate".

You're missing a so called certificate used to make sure that you're actually download from github. To skip this control/check you need to add the command line option --no-check-certificate to wget.

Note:If a script fails for the same reason you will have to edit the script and add that command line option to the wget command (in the script).

This means that the command line above should be something like:

$ wget --no-check-certificate https://raw.githubusercontent.com/progund/tig015-weekly/master/weekly02/get_latest_jar.sh

Problems running a script syntax error near unexpected token `$'{\r'

If you get this error from running a script:

syntax error near unexpected token `$'{\r''

...then you probably have modified the script in a Windows editor so that it got CRLF line endings (the Windows style line endings).

You can convert the file (any text file, actually) from windows format to Unix format using dos2unix (install it in Cygwin if you are running Cygwin). If you are using a text editor, you can always change the line endings in the file:

  • Atom: Look for the letters CRLF at the bottom of the editor, click on it and change it to LF only.
  • Notepad++: Edit -> EOL Conversion -> Unix Format
  • Sublime: View -> Line Endings... (Yes, it is under the View menu, don't ask why)

If you are running Mac OS and the above doesn't help and you get something like illegal byte sequence 'LF', then you can try to run the script like this:

$ LC_ALL=C ./randomcar.sh 1000

What you did was to change the locale temporarily for the command line to use C (which is a general locale).

Read more:

Problems running a script `<!DOCTYPE html>'

If you get this error from running a script:

./produce-json-static.sh: line 7: syntax error near unexpected token `newline'
./produce-json-static.sh: line 7: `<!DOCTYPE html>'

it probably means that you failed downloading the script and instead downloaded an HTML page by mistake.

If the script you tried to download came from Github, open the link to the script in a browser. If the link leads to a page on Github showing a lot more than the script, then click at the button on the page labeled "Raw". This is the link to the actual, 'raw', script on Github. Please notify the authors of this Wiki that they have a faulty link pointing not to the script on Github, but to a web page displaying the script (and more).

It is always a good habit to examine scripts that don't work by using file:

$ ./produce-json-static.sh 
./produce-json-static.sh: line 1: syntax error near unexpected token `newline'
./produce-json-static.sh: line 1: `<!DOCTYPE html>'
$ file ./produce-json-static.sh
./produce-json-static.sh: HTML document, UTF-8 Unicode text, with very long lines

What are .DS_Store files, and what are they doing in my file system? (MacOS X users)

Apple's Finder (file browser) creates these annoyances when you browse a directory using Finder. If you, like us, find these files stupid and annoying, feel free to remove them using the rm command. Please do so in particular before zipping files and folders which you should hand in to the teachers as part of some task or assignment. They are simply in the way and provide no use what so ever for the teachers.

You can read more about these stupid files in this old article.

To read about .DS_Store, we refer to wikipedia's article on the subject.