What is the exact use of & in shell scripting? [closed] - shell

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I'm very new to shell scripting. I have a basic doubt of what is the use of & in shell scripting? That is doing something like this :
commands arg &
Waits to give more input. But what is its exact use? How should I use it in real world?

As you have used it there, it runs commands arg in the background, disconnected from your keyboard, and the shell immediately asks you for its next command.
As for the real world, I use it when I have a command that will take some significant time to run but does not require any input from me and will put all of its output into a file, for example
# walk through the whole filesystem looking for a particular filename
find / -name 'obscure.filename' -print > /tmp/found-it &

Related

How can a script know whether a user can input or not? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Is there a way a bash script (or whatever language) can determine whether its being run in a headless way? I want to know whether a user can input or not. If they can, I am going to ask them something.
From man bash:
An interactive shell is one started without non-option arguments and without the -c option whose standard input and
error are both connected to terminals (as determined by isatty(3)), or one started with the -i option. PS1 is set and
$- includes i if bash is interactive, allowing a shell script or a startup file to test this state.

bash can you write a script file that modifies itself for the next time it is called? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Let's say as an example that I wanted to write a script file that not only kept count of how many times it's been called, but would average the time that it has been called since the first time. and to do this without relying on environmental variables or secondary files. And report the number of lapsed days as well. This would mean that it would have to be self-modifying. Now when a script is loaded and executed, the saved version on disk can be changed without effecting the copy in memory, so that works, or should. Just change the copy on file.
But making it happen can be a bit tricky. So what is your best solution?
Sounds a bit weird, but a bash script is just text, so you can always edit it IF you have permission:
Take this example:
#!/bin/bash
VAR=1
let VAR=VAR+1
echo Set to $VAR
perl -pi -e 's/^VAR=\d+/VAR='$VAR'/' $0
Trying it out:
$ /tmp/foo.sh
Set to 9
$ /tmp/foo.sh
Set to 10
$ /tmp/foo.sh
Set to 11

In Unix Shell, how can I navigate, aka move the cursor position more than one character at a time? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I'm using SSH Shell to work with a Unix system and I have a really long query in the command that I would like to edit.
However the part I need to edit is way at the beginning and the only way for me to get there is to hold down the left arrow button for about a minute (each time I need to make a new edit).
Is there a faster way to navigate?
If you are using a ahell with emacs mode command line editing commands you can use the following movement commands (Here a complete reference):
Ctrl-a Move to the start of the line.
Ctrl-e Move to the end of the line.
Alt-f Move forward a word, where a word is composed of letters and digits.
Alt-b Move backward a word.
You may be a bit faster with these.
To get emacs mode with the different shells:
bash: you have to do nothing
ksh: invoke with ksh -o emacs
tcsh: call bindkey -e
Edit: to find out the shell currently in use:
ps -p $$
Example output:
PID TTY TIME CMD
3701 pts/1 00:00:00 ksh

How to repeat last command parameter in ZSH [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I often need to move file from one location to other, but it requires copying and pasting huge part of the command. For example:
mv ~/Projects/foo/bar/baz.img ~/Projects/foo/bar/fiz.dmg
Is it possible after entering ~/Projects/foo/bar/baz.iso part of above command to use some shortcut that duplicates it so I can change the very end to fiz.dmg?
You don’t need this:
mv ~/Projects/foo/bar/baz.img ~/Projects/foo/bar/fiz.dmg
easily turns into
mv ~/Projects/foo/bar/{baz,fiz}.img
. Note: zsh completion is still available when you write { (unless you have a habit of writing closing } right away) if you want it.
I actually discovered one possible solution. Just press:
Ctrl+W few times and then Ctrl+Y two times (and space between pastes).

How to cancel long output in the terminal? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
How to exit from the long output of a terminal command or a git command like git log or git branch -a?
How to avoid that I have to press enter untill the output of the command is complete?
git log uses more or less for paging the output.
You can press q to stop more/less and drop the remaining output.
Ctrl-C is the entire answer, though it's too short to go in this field.

Resources