You can't "skip" a running process with bash. According to this page from bash docs, even if it had a skip behavior for a signal, it would wait until the process finished execution to issue the skip, effectively skipping only the following instruction. You can, though, change the way your script runs so that it kills the unzip process for you, if running on background, upon receiving a SIGINT for example (guide).
Answer from Zip on Stack ExchangeYou can't "skip" a running process with bash. According to this page from bash docs, even if it had a skip behavior for a signal, it would wait until the process finished execution to issue the skip, effectively skipping only the following instruction. You can, though, change the way your script runs so that it kills the unzip process for you, if running on background, upon receiving a SIGINT for example (guide).
You have the option of capturing the SIGINT signal using Bash's builtin trap:
trap [COMMANDS] [SIGNALS]
You can, for example, capture a SIGINT that was sent to the script in the foreground to send a signal to the process that was forked from the script.
In this case, however, you'll need the PID of the forked process. You can make use of Bash's $! variable, but it only works for processes that were forked to the background.
As a workaround, to preserve the synchronous nature of your script, you can explicitly call wait on the returned PID.
#!/usr/bin/env bash
function skip() {
kill -SIGTERM $!
}
trap skip SIGINT
ping ... &
wait $!
wget ... &
wait $!
unzip ... &
wait $!
To implement those the two logics:
- If no arguments at all, run all functions
- For each argument passed, skip some function execution
You can do it like this:
#!/bin/bash
function func_i {
echo "I am i."
}
function func_b {
echo "I am b."
}
function main {
# Check if there are no arguments, run all functions and exit.
if [ $# -eq 0 ]; then
func_i
func_b
exit 0
fi
# Parse arguments -i and -b, marking them for no execution if they are passed to the script.
proc_i=true
proc_b=true
while getopts "ib" OPTION; do
case $OPTION in
i)
proc_i=false
;;
b)
proc_b=false
;;
*)
echo "Incorrect options provided"
exit 1
;;
esac
done
# Execute whatever function is marked for run.
if $proc_i; then func_i; fi
if $proc_b; then func_b; fi
}
main "$@"
Some explanations:
$# returns the number of arguments passed to the script. If $# is equal to 0, then no arguments were passed to the script.
getops accepts switches -i and -b, all other switches will result in error handled in the *) case.
You could black list items from the list of functions that are called by default. Something like:
#!/bin/bash
list='a b c d e f g h i'
# define some functions
for name in $list; do
eval "func_$name() { echo func_$name called with arg \$1; }"
done
# black list items from list
for x; do
list=$(echo "$list" | tr -d ${x#-})
done
for name in $list; do
func_$name $name
done
But frankly it makes more sense to do something like:
$ cat script.sh
#!/bin/bash
list='a b c d e f g h i'
test $# = 0 && set -- $list # set default list of functions to call
# define some function
for name in $list; do
eval "func_$name() { echo func_$name called with arg \$1; }"
done
for name; do
func_$name $name
done
$ bash ./script.sh
func_a called with arg a
func_b called with arg b
func_c called with arg c
func_d called with arg d
func_e called with arg e
func_f called with arg f
func_g called with arg g
func_h called with arg h
func_i called with arg i
$ bash ./script.sh c g
func_c called with arg c
func_g called with arg g
To skip operations in UNIX shell
linux - Print a file, skipping the first X lines, in Bash - Stack Overflow
To skip operations in UNIX shell
go to / skip in script
Videos
At last I've found it:
./script.sh "para1" "" "para3"
My teacher should be happy with this. He won't get anything else from me.
There's no thing like skipping as you would like. Arguments are just numbered, if there's no second parameter then… well, there's actually no third parameter. So either you explicitly give an emptry string as second parameter (if your exercise permits this), or you check how many parameters are given and in case it's two, take the second parameter as third (since it's a school assignment, I won't go into detail here, just have a look at the description of the $# special variable in your shell's manual).
Use tail. Some examples:
$ tail file.log
< Last 10 lines of file.log >
To SKIP the first N lines:
$ tail -n +<N+1> <filename>
< filename, excluding first N lines. >
For instance, to skip the first 10 lines:
$ tail -n +11 file.log
< file.log, starting at line 11, after skipping the first 10 lines. >
To see the last N lines, omit the "+":
$ tail -n <N> <filename>
< last N lines of file. >
Easiest way I found to remove the first ten lines of a file:
$ sed 1,10d file.txt
In the general case where X is the number of initial lines to delete, credit to commenters and editors for this:
$ sed 1,Xd file.txt
Yes, it is possible to send all occurring errors "to the trash" by using
command > /dev/null 2>&1
or
command &>/dev/null
For more detailed information visit e.g. cyberciti or do a quick google search, then you will probably find more detailed tutorials.
Commands which are part of the bash syntax, have to be right in a script. But you are speaking about the result of the execution of a program called by this bash script. $? is the value returned by the last executed command. You can use it to make decisions in the following part of the bash script. Unfortunately, according to man pages of the host and dig commands, there is no useful code returned.
You have the general capability to redirect the output of the dig or host commands and to analyze it using commands like grep or awk. But you are depending on the exact syntax of the output of these programs. This syntax can change in future releases of these programs.
Please note a discrepancy in your question. Your host command is asking about example-domain.com and the cited output is related to exa--mple.com. Further, it is better to always ask about absolute domain names, i.e. ending with a period.
To delete a word before the cursor, press Ctrl+w. This is actually kind of a "cut to clipboard". Use Ctrl+y ("yank") to paste it back elsewhere. Ctrl+k will cut to the end of the line, Ctrl+u to the start of the line.
To skip over a word backwards, use Alt+b. Use Alt+f to skip over a word forwards.
These keys and many others universally work in bash in your terminal emulator, in a virtual console or over an ssh connection.
In the gnome terminal emulator, Ctrl+Left/right will jump over words left or right. Alt+Backspace will delete the word on the left, Ctrl+Del will delete the word to the right from the current position.
moving:
- to the end of the command: ctrl-e
- to the begin of the command: ctrl-a
- forward a word: alt-f
- backword a word: alt-b
deleting:
- from current cursor position to the end of word: ald-d
- from current cursor position to the begin of word: clt-w
example:
- touch .gitignore
- cltr-a (go to the begin)
- alt-d (delete "touch")
- type "code" => code .gitignore
Ok, so a lot of times I forget to type sudo, or add some random letter to it. Is there a hot key or a way to jump to the begging of the line?
Also, while I'm asking, is there any other hotkeys you think I should know about?
Thanks