Linux Shell Scripting
This is a hybrid course.
ssh username@machine.uni.fi
Remote SSH to a native Linux
bit of code pasted
Materials: https://aaltoscicomp.github.io/linux-shell/quoting-substitution-aliases/
exec bash
Does it help?chsh -s /bin/bash
and then open a new terminal (it changes your default shell to bash)chsh
will change the default shell, so if you're running on e.g. OSX, changing it might not be a good idea. On Aalto linux machines bash
is recommended. You can also run SHELL=bash bash
if you want to change the $SHELL
-variable for this process only. Simotoday
when running today=date
versus today=$(date)
. I don't wanna spoil the answer on presemo :Dhostname
https://man7.org/linux/man-pages/man1/hostname.1.html
-s
-flag returns the short version of the hostname. SimoExercise 2.1 20mins, at least first 4 bullet points
https://aaltoscicomp.github.io/linux-shell/quoting-substitution-aliases/#exercise-2-1
Credits (1 ECTS or even 2 if you are very motivated)
the comparison: is just the file names or it looks into the file content to comapre if they are the same?
diff
example? Diff compares the content of txt files. In the example diff <(ls dir1) <(ls dir2)
the part (ls dir1)
is like a file so that ls dir1 > fileA.txt
. So it would be like comparing two txt files diff fileA.txt fileB.txt
created with the commands surrounded by ( )Why does echo $({1..5})
not work?
$( )
you want to have a command, but {1..5}
is not a command. The dollar+roundbrackets does command substitution$( )
expects a command, while {1..5} gives you a string like 1 2 3 4 5; echo {1..5}
works as it shoud"Use the example in the text above to send du -hs * .[!.]* | sort -h
output to yourself." What do you mean to yourself? To STDOUT?
When trying to execute u -hs .[!.]* * | sort -
I get error message cannot access .[!.]*
: No such file or directory
touch .hiddenfile.txt
and re-run the command again and you will see that it won't give the error anymore.2>/dev/null
, overall yes, du can't find any file that starts with a dot (hidden files) and complainsWhat does the end of the mailing example mean: ; mv listing listing.$(date +"%Y-%m-%d-%H-%M");
? I assume that in the beginning the command creates temporary file named listing
and then it is used later in the command?
I feel that I am repeating myself in the diff task, namely using stat -c "%y" filename
twice, is there anywya to avoid this?
Testing mail gets stuck asking for cc for some reason, I tried running mail -s "this is the subject" "my address"
cannot send message: Process exited with a non-zero status
I'm stuck in trying to test whether a directory exists, what commands should I use? echo $(path) and redirect it to /dev/null ? Is echo ls $(path) > /dev/null && echo exists || echo "doesn't exist"
close ?
Can't mail myself, my system does not allow it:
^^^ Most likely you are on a remote server, for example at Aalto kosh they blocked use of "mail" from the terminal. This change happened quite recently, maybe we need to re-think this exercise :)
It would be good to see the exercise solution also here: https://users.aalto.fi/~degtyai1/shell/bash_history.2022-03-21
I get this:
stat: illegal option – c
usage: stat [-FlLnqrsx] [-f format] [-t timefmt] [file …]
https://aaltoscicomp.github.io/linux-shell/variables-functions-environments/
In the var=26_file.ext; echo ${var%.[a-z][a-z][a-z]} why three of the [a-z]'s?
..
..
Will the recordings be in youtube before next class? or at teh end.
Next class is on Friday right? (I thought I heard you say Wednesday)
Write anything that comes to your mind! What was good? What can be improved?
-In addition to "It's ok, i can manage", I felt that it was a bit fast though. If I missed something, it moved forward quickly. Luckily there will be the recordings and the scripts available.
Recordings which might have your voice or username are only shared internally with the participants. If we upload to youtube, then no voices / names / etc will appear there.
https://aaltoscicomp.github.io/linux-shell/variables-functions-environments/#functions
question you can write hre…
We can see the screen.
Is this link still valid today? "https://users.aalto.fi/~degtyai1/shell/"
Why is it source functions
instead of source ~/bin/functions
source
looks for the specified file on the path and by default the ~/bin/
folder is on the pathI am still getting the same output as the presenter, meaning host part has not been recognized even I added the echo. (m3:domain: command not found)
source functions
again? Otherwise your modified function will not get loadedWhat does me stands for?
You did not source function this time. Was that intentional?
source
the file time every time you make changes to a functionWhat should the output be for filepath function? If I add a file as argument I just get that filename back.
I recive this error while running spaceusage:
du: cannot access '.[!.]*2': No such file or directory
du -hs ${1:-.}/* ${1:-.}/.[!.]* 2>/dev/null | sort -h
In zoom we cannot hear what you talk about in the in-person room, so if the questions are interesting please repeat them. Thanks :)
what is mean by path/to/
-filebasename path/to/archive.tar.gz what should i put in instead of path/to/
Okay, pretty clear, thanks. One more question, is there any special function that works like grep to find the files' location?
locate
command is installed in your computer. It is very efficient.find
can be resource-hungry.which ssh
would work
why word variable put into "{}"? cannot be only in form of $word?
${word}ing
to refer to the environment variable word
and add ing instead of $wording
where you would refer to the environment variable wording
why at end of each line &&\ has been added, mine is working without this!
Are we (non-aalto ones) also supposed to have triton account for the course?
In the fork-bomb function, why the functions output has to be piped to itself? Would not it be sufficient just call the function and send it to background like this: { bomb $; }
Why would we want to kill the system? Why would that be needed? If there is overload. I might have missed some explanation, but still seems unclear to me.
whatever file i type it says exists:
$ [[ -filesadfasdfasd ]] && echo file exists
file exists
[[ -file- ]]
double checked it, seems okay[[ -f filename ]]
Some links to learn these regex more would be great.
man grep
has a referencerecieving this error even tho i created the file with nano: chmod: cannot access 'if-test.sh: No such file or directory
I got this:
Can't add archive to itself(base)
./tarit.v1.sh ../../linux_shell_scripting_spring_2022
Or, replace the shebang:
#!/bin/bash -x
that does effectively the same thing. Bash will write out what it does.
please could you also show the script for this (again) if bin was not part of path variable?
I followed all steps but i am still getting "cx: command not found"
## Getting started
https://aaltoscicomp.github.io/linux-shell/loops/
questions here
…
.
if you use more exit then the same number 1 applies?
Why are the results different when using
- n=10; ((n++))
then echo $n and
- n=10; echo $((n++))
?
n++
returns n
and then increments (post-increment); in just like e.g. in, C there is pre-increment separately: ++c
vs. c++
, see https://tecadmin.net/bash-increment-decrement-operators/Could you do the scripts on demospace again? I think you are on triton now?
The parameters or looping variable contents in the for loops: in your examples they have been "horizontal vectors", space separated. When or how should one use new-line separated lists?
Please ask for assistance! :-)
This is the end of the document, WRITE ABOVE THIS LINE ^^
HackMD can feel slow if more than 100 participants are editing at the same time: If you do not need to write, please switch to "view mode" by clicking the eye icon on top left