Questions tagged [scripting]

A script is a sequence of commands or instructions that are executed by an interpreter program rather than compiled into a standalone executable program.

Filter by
Sorted by
Tagged with
17 votes
4 answers
12k views

What is the function of bash shebang? [duplicate]

What is the function of bash shebang? What is the difference between executing a file using ./file or sh file? How does bash understand it?
Daniel's user avatar
  • 771
17 votes
1 answer
49k views

What does "if [ -e $name ]" mean? Where $name is a path to a directory

In the if [ -e $name ], what condition check is -e? $name is a variable of a path to a directory.
user277585's user avatar
17 votes
3 answers
13k views

Is there a way to simulate a "Close" event on various windows using the terminal?

I answered on Ask Ubuntu Quit all instances of gnome-terminal via a command but as you all can read gnome-terminal didn't seems to have a SIGcall I could use to simulate this "Close" event. So this ...
Braiam's user avatar
  • 36k
17 votes
2 answers
9k views

shell script: use sudo inside it vs run it with sudo?

When writing a shell script, in which some but not all commands in it need superuser privileges, shall I add sudo to those commands which need superuser privileges, and run the shell script without ...
Tim's user avatar
  • 102k
16 votes
4 answers
5k views

Does Linux support invoking a program directly via its inode number?

I’m asking because string comparisons are slow, but indexing is fast, and a lot of scripts I write are in bash, which to my knowledge performs a full string lookup for every executable call. All those ...
Jacob Valdez's user avatar
16 votes
8 answers
18k views

How to remove multiple blank lines from a file?

I have some text-files I use to take notes in - just plain text, usually just using cat >> file. Occasionally I use a blank line or two (just return - the new-line character) to specify a new ...
Baard Kopperud's user avatar
16 votes
6 answers
55k views

How to send multiple commands to sftp using one line

The following command sends one command to sftp using one line: sftp -o PasswordAuthentication=no user@host" <<<"lcd /home" How to send multiple lines to sftp using one line. Is there a way ...
Radnaskela Samot's user avatar
16 votes
4 answers
82k views

bash - get pid for a script using the script filename

I have two scripts: running_script script_one I need to get the PID for the/any instances of running_script running under a username, and then pkill to stop the running_script and daughter processes....
will's user avatar
  • 500
16 votes
3 answers
5k views

How do I do an N-way diff?

How do I diff the output of multiple commands? vimdiff can support up to four files, but diff itself seems to support exactly two files. Is it directly possible with some variant of diff, or do I ...
muru's user avatar
  • 72.9k
16 votes
1 answer
36k views

Match keys with regex in jq [closed]

I'm writing a script that analyses JSON input (jsonfile): { "key11":1010,"key11_yes":13,"key11_no":14, "key12":12120,"key12_yes":9,"key12_no":25, "key13":12103,"key13_yes":13,"key13_no":20 ...
pyramid13's user avatar
  • 639
16 votes
3 answers
3k views

How to pipe the list of commands displayed by "tab complete"?

When using commands in bash I like the double-Tab option to display the available commands. Some commands have more possible matches than others. Example: here is just some of the output after typing ...
cwd's user avatar
  • 45.4k
16 votes
4 answers
6k views

How to perform a sed in-place substitution that only creates backups of files that were changed?

I ran the following to replace a term used in all files in the current working directory: $ find . -type f -print0 | xargs -0 sed -i'.bup' -e's/Ms. Johnson/Mrs. Melbin/g' This performed the word ...
Belmin Fernandez's user avatar
16 votes
6 answers
22k views

What are good online resources for learning shell scripting? [closed]

I am interested in learning shell script ompletely. Can anyone suggest some good online resources?
oldrock's user avatar
  • 279
16 votes
2 answers
18k views

assign shortcut key to run a script

I've written a script to change the brightness of my screen. It's useful when I am working on text mode and shortcut keys of my keyboard to change brightness doesn't work. Is it possible to run this ...
M0εiπ's user avatar
  • 1,257
16 votes
3 answers
2k views

When to use redirection to stderr in shell scripts

I know that well-behaved utilities like grep output "normal" messages to stdout, and error messages to stderr. $ grep '^foo' file1 file2 file1:foo grep: file2: No such file or directory When I'm ...
glts's user avatar
  • 582
16 votes
2 answers
14k views

Get directory from which script was called from

I have a script doSmth in /usr/bin. Is it possible to get and print the directory the script was called from? So if I call doSmth from /home/me the output will be /home/me.
c0dehunter's user avatar
16 votes
1 answer
10k views

Making a progressbar with "dialog" from rsync output

I'm looking for a way to filter/redirect rsync output in a manner where it can be fed to the "dialog --gauge" command, so I can get a nice looking progressbar during file sync. Currently I have only ...
chr1573r's user avatar
  • 163
15 votes
3 answers
4k views

Call a Linux syscall from a scripting language

I want to call a Linux syscall (or at least the libc wrapper) directly from a scripting language. I don't care what scripting language - it's just important that it not be compiled (the reason ...
joshlf's user avatar
  • 365
15 votes
3 answers
7k views

Can I use a variable in a Bash brace expansion?

Below is some sort of pseudo-code for what I'm trying to accomplish: #!/bin/bash # I already have the variable below figured out (positive integer): numlines=$([returns number of lines containing ...
rubynorails's user avatar
  • 2,293
15 votes
5 answers
4k views

How to print own script name in mawk?

In bash $0 contains the name of the script, but in awk if I make a script named myscript.awk with the following content: #!/usr/bin/awk -f BEGIN{ print ARGV[0] } and run it, it will only print "awk"....
cipper's user avatar
  • 363
15 votes
4 answers
69k views

How do I create sequentially numbered file names in bash?

I need a script that will create a file with the next file in a sequence. Each execution of the script should only create one file and the script could be run zero or more times on any given day. The ...
Greg B's user avatar
  • 321
15 votes
6 answers
11k views

Best way run a command on each file in a directory tree

There appear be a number of ways to do this including loops in shell script, find and xargs. Which of these is best, and which is most portable?
Brian Lyttle's user avatar
  • 2,651
15 votes
4 answers
6k views

How can a Bash script tell how it was run?

I have a Bash script I was trying to make to help me run a rather complex command with small changes that it would ask me about through echo and read. I have found solutions to force it to run a ...
Aescula's user avatar
  • 363
15 votes
1 answer
2k views

How variables inside braces are evaluated

I'm new to shell scripting and I came across these expressions ${var:-val} and ${var-val} ${var:+val} and ${var+val} ${var:=val} and ${var=val} so how are they evaluated and what are the ...
WLION's user avatar
  • 295
14 votes
4 answers
14k views

How do I get 0-padded numbers in {} (brace expansion)?

I'm trying to write a simple script to retrieve memory and swap usage from a list of hosts. Currently, the only way I've been able to achieve this is to write 3 separate scripts: for a in {1..9}; do ...
Matthew Perrott's user avatar
14 votes
3 answers
75k views

Shell script wait for background command

I am writing a script, but there is something I need that I can't find a way to do it... I need to make a command in background "command1 &" and then somewhere in the script I need to wait for it ...
Joao Macau's user avatar
14 votes
3 answers
8k views

Maintain (or restore) file permissions when replacing file

I have a command that accepts a file as an argument, modifies the file, then writes it to the file name specified in the second argument. I'll call that program modifyfile. I wanted it to work "in ...
Stephen Ostermiller's user avatar
14 votes
4 answers
31k views

grep -- removing text after delimiter token

I have a file in which I need to eliminate everything after the first ; on every line. So a file like this: sdfsdsdf; fsdfsddf;sdfsd; Will result in this: sdfsdsdf fsdfsddf I have looked into ...
myusuf3's user avatar
  • 1,425
14 votes
1 answer
24k views

Execute command or function when SIGINT or SIGTERM is send to the parent script itself, not the child processes

Let's say I have this script.sh #!/bin/bash exit_script() { echo "Printing something special!" echo "Maybe executing other commands!" kill -- -$$ # Sends SIGTERM to child/sub processes } ...
bosa djo's user avatar
  • 143
14 votes
1 answer
13k views

Export passwords from the `pass` password manager

I've been using the pass password manager for a year or so now, and it's awesome, but sometimes I like to test other password managers and then I need my passwords back. So far I've just been adding ...
greduan's user avatar
  • 503
14 votes
3 answers
21k views

what are practical uses of stdbuf or nohup?

I have to finish a writeup of a few coreutils commands for a course at the moment, and I can't think of a starting point for a small practical code example that demonstrates the potential uses of ...
kav's user avatar
  • 143
14 votes
1 answer
3k views

Bash is automatically reloading (injecting) updates into a running script upon saving it: Why? Any practical use?

I was writing a bash script, and happened to updated the code (saved the script file to disk) while the script was waiting for some input in a while loop. After I returned to the terminal and ...
aularon's user avatar
  • 2,042
14 votes
3 answers
12k views

Check bash/shell script dependencies

Is there a method/command to check for the dependencies of a bash script? In other words, a response to this question : Which libraries should a user install to run the script? I can do this ...
4m1nh4j1's user avatar
  • 1,873
14 votes
2 answers
11k views

is there a way to get the url from current tab in google-chrome?

I used to do that on firefox using a python command, but recently something changed and I can't get the URL anymore! What used to work with firefox: #!/bin/bash current_tab_num () { python2 <&...
Yunus's user avatar
  • 1,684
14 votes
4 answers
1k views

A shell-like environment for binary processing

This question came to me a couple of times before, now in response to the question Loop through binary data chunks from stdin in Bash Answers given in https://stackoverflow.com/questions/993434/what-...
orion's user avatar
  • 12.5k
13 votes
7 answers
18k views

bash: how do you return file extensions?

I want to write a script to return just the extension of the input file name. For example, textfile.txt should return txt. I'm new to Linux and bash, and I'm trying to learn how to write basic ...
user138438's user avatar
13 votes
4 answers
16k views

Is it possible to execute a VIM script in a non-interactive mode?

The Gnats bug tracker has a command (edit-pr) that opens a ticket in $EDITOR. My $EDITOR is VIM, and when this command opens the ticket in $EDITOR, I'd like to automatically perform some scripted ...
Matt Pfefferle's user avatar
13 votes
5 answers
4k views

"Store" a remote SSH session?

I am trying to do these in a script. I have to run some commands on a remote host. Currently, I am doing this: ssh root@host 'bash -s' < command1 ssh root@host 'bash -s' < command2 ssh root@...
SayantanRC's user avatar
13 votes
3 answers
3k views

How to make those old-school GUIs (like the debian/arch installer)

I've been doing a lot of installs lately, and when possible I usually opt for the text install from the Live CD's GRUB menu. Most installers looks like this: I always thought these GUIs (if you can ...
nopcorn's user avatar
  • 9,559
13 votes
6 answers
113k views

How to set Ruby GEM_HOME and GEM_PATH

I'm a NOVICE user of Linux and using Linux mint 17.1 I've reinstalled Linux Mint 20 time in last 3 days for that issue but could not fix it I'm trying to install ruby on rails using rvm what ...
Amjad's user avatar
  • 239
13 votes
3 answers
1k views

How do you continue a sentence with a command inside

For example, I have been trying to say "There are 10 people online at the moment" in my script file. I can never seem to get the command working without the "people online at the moment" part on the ...
techiegeek's user avatar
13 votes
2 answers
6k views

Multiline command : comment out one line

I like to use the following format in scripts for commands with a lot of parameters (for readability): docker run \ --rm \ -u root \ -p 8080:8080 \ -v jenkins-data:/var/jenkins_home \ -v /var/...
Chirlo's user avatar
  • 377
13 votes
4 answers
11k views

remove title bar of another program

I have developed a program with glut library and want to remove its title bar. Since its impossible to remove the title bar from glut library I have to use an external tool to remove its title bar. ...
Sponge Comrade's user avatar
13 votes
2 answers
27k views

How to make custom zsh script executable automatically?

I must be missing something incredibly simple about how to do this, but I have a simple script: extract () { if [ -f $1 ] ; then case $1 in *.tar.bz2) tar xvjf $1 ;; *.tar.gz) ...
Ryan David Ward's user avatar
13 votes
5 answers
25k views

Exiting a shell script with nested loops

I have a shell script with nested loops and just found out that "exit" doesn't really exit the script, but only the current loop. Is there another way to completely exit the script on a certain error ...
user923487's user avatar
13 votes
2 answers
77k views

What's the best way to edit a file with a bash script?

Using /bin/bash on RHEL 5.8 and I want to automate editing a file. I need the script to search the file and replace a line in the file. Line example: Other line Current date 01121990 Other line ...
Paul's user avatar
  • 131
13 votes
3 answers
49k views

How to add Multiple Searches in AWK command

Im trying to search for the files Created in June,July and August months. I used this method ls -lrth|awk '/[Jun][Jul][Aug]/ {print}' but it ain't working.
OmiPenguin's user avatar
  • 4,318
13 votes
1 answer
39k views

Bash getopts, mandatory arguments

I am working with a pretty simple bash script, but I am facing a problem which I can't resolve: I have myscript.sh with three parameters, -u, -h, and -p. Both -u and -h are mandatory, needed for the ...
Learner33's user avatar
  • 133
13 votes
2 answers
16k views

Problem while running "newgrp" command in script

I want to change the working group in my script. So I wrote a line newgrp test_grp1 in my script. But my script is automatically exited after running this line. Are there any solution to overcome this ...
chanchal1987's user avatar
13 votes
2 answers
7k views

Piping for loop output prevents local variable modification

I'm trying to write a simple bash function that takes, as its arguments, a number of files and/or directories. It should: Fully qualify the filenames. Sort them. Remove duplicates. Print all that ...
tjm's user avatar
  • 233

1 2 3
4
5
90