Skip to main content

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
267 votes
11 answers
229k views

Allow setuid on shell scripts

The setuid permission bit tells Linux to run a program with the effective user id of the owner instead of the executor: > cat setuid-test.c #include <stdio.h> #include <unistd.h> int ...
Michael Mrozek's user avatar
153 votes
3 answers
35k views

Have backticks (i.e. `cmd`) in *sh shells been deprecated?

I've seen this comment many times on Unix & Linux as well as on other sites that use the phrasing "backticks have been deprecated", with respect to shells such as Bash & Zsh. Is this ...
slm's user avatar
  • 372k
114 votes
5 answers
86k views

Does the shebang determine the shell which runs the script?

This may be a silly question, but I ask it still. If I have declared a shebang #!/bin/bash in the beginning of my_shell_script.sh, so do I always have to invoke this script using bash [my@comp]$...
jrara's user avatar
  • 2,089
148 votes
11 answers
402k views

Script to change current directory (cd, pwd)

I want to run a script to simply change the current working directory: #!/bin/bash cd web/www/project But, after I run it, the current pwd remains unchanged! How can I do that?
Sony Santos's user avatar
  • 1,583
236 votes
9 answers
313k views

Looping through files with spaces in the names? [duplicate]

I wrote the following script to diff the outputs of two directores with all the same files in them as such: #!/bin/bash for file in `find . -name "*.csv"` do echo "file = $file"; diff $...
Amir Afghani's user avatar
  • 7,263
64 votes
16 answers
279k views

Automatically run commands over SSH on many servers

There is a list of IP addresses in a .txt file, ex.: 1.1.1.1 2.2.2.2 3.3.3.3 Behind every IP address there is a server, and on every server there is an sshd running on port 22. Not every server is ...
LanceBaynes's user avatar
  • 40.6k
105 votes
7 answers
239k views

Using while loop to ssh to multiple servers

I have a file servers.txt, with list of servers: server1.mydomain.com server2.mydomain.com server3.mydomain.com when I read the file line by line with while and echo each line, all works as expected....
Martin Vegter's user avatar
45 votes
14 answers
32k views

Using notify-send with cron

I am using Arch Linux with KDE/Awesome WM. I am trying to get notify-send to work with cron. I have tried setting DISPLAY/XAUTHORITY variables, and running notify-send with "sudo -u", all without ...
justsomeone's user avatar
657 votes
4 answers
501k views

Using "${a:-b}" for variable assignment in scripts

I have been looking at a few scripts other people wrote (specifically Red Hat), and a lot of their variables are assigned using the following notation VARIABLE1="${VARIABLE1:-some_val}" or some expand ...
Justin Garrison's user avatar
31 votes
4 answers
143k views

Storing output of command in shell variable [duplicate]

I have an operation using cut that I would like to assign result to a variable var4=echo ztemp.xml |cut -f1 -d '.' I get the error: ztemp.xml is not a command The value of var4 never gets ...
Vass's user avatar
  • 5,401
12 votes
4 answers
45k views

How to use wc and piping to find how many files and directories are in a certain directory?

How can I use word counter (wc) and piping to count how many files or directories are in the /usr/bin directory?
cash's user avatar
  • 183
7 votes
4 answers
13k views

Print two files in two columns side-by-side

I want to output two text files in two columns — one on the left side and other one on the right. paste doesn't solve the problem, because it only insert a character as delimiter, so if the first file ...
belkka's user avatar
  • 483
49 votes
7 answers
34k views

What Unix commands can be used as a semaphore/lock?

I want to run multiple Bash shell scripts in parallel. However, I want to avoid race conditions. What Unix commands are truly atomic that I could use for this purpose, and how can I use them?
Larry Wang's user avatar
  • 2,705
99 votes
8 answers
48k views

Multiple arguments in shebang

I am wondering whether there is a general way of passing multiple options to an executable via the shebang line (#!). I use NixOS, and the first part of the shebang in any script I write is usually /...
Rastapopoulos's user avatar
79 votes
3 answers
31k views

Shebang line with `#!/usr/bin/env command --argument` fails on Linux

I've got a simple script: #!/usr/bin/env ruby --verbose # script.rb puts "hi" On my OSX box, it runs fine: osx% ./script.rb hi However, on my linux box, it throws an error linux% ./script.rb /usr/...
rampion's user avatar
  • 1,669
652 votes
24 answers
727k views

Repeat a Unix command every x seconds forever

There's a built-in Unix command repeat whose first argument is the number of times to repeat a command, where the command (with any arguments) is specified by the remaining arguments to repeat. For ...
dreeves's user avatar
  • 6,669
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
11 votes
1 answer
12k views

How does this find command using "find ... -exec sh -c '...' sh {} +" work?

@StephaneChazelas posted the following solution to this Q&A: Having some trouble using “find -exec {} +”. $ find . -iname "*.extension" -exec sh -c ' exec <command> "$@" <additional ...
slm's user avatar
  • 372k
4 votes
6 answers
39k views

Check for Process if Same is Running

Is there a script or a way in linux that when I try to execute a shell script/process, if the same is running, it will prompt that same is running and will exit otherwise it will continue.
user60216's user avatar
  • 325
144 votes
3 answers
562k views

Pass command line arguments to bash script

I am new to bash script programming. I want to implement a bash script 'deploymLog', which accepts as input one string argument(name). [root@localhost Desktop]# ./deploymLog.sh name here I want to ...
user avatar
118 votes
2 answers
382k views

su options - running command as another user

I was wondering how to run a command as another user from a script. I have the script's owner set as root. I also have the following command being run within the script to run the command as the ...
myusuf3's user avatar
  • 1,425
46 votes
7 answers
4k views

What's a good mnemonic for shell double vs. single quotes?

I don't do enough scripting to remember, without looking up, whether double or single quotes result in a Unix variable being substituted. I definitely understand what is going on. My question is does ...
Dave Potts's user avatar
199 votes
6 answers
508k views

how can I add (subtract, etc.) two numbers with bash?

I can read the numbers and operation in with: echo "First number please" read num1 echo "Second number please" read num2 echo "Operation?" read op but then all my attempts to add the numbers fail: ...
Michael Durrant's user avatar
131 votes
3 answers
96k views

What does it mean to have a $"dollarsign-prefixed string" in a script?

I just saw this in an init script: echo $"Stopping Apache" What is that dollar-sign for? My research so far: I found this in the bash manual: extquote If set, $'string' and $"string&...
Ed Brannin's user avatar
  • 1,413
48 votes
3 answers
63k views

Using the setuid bit properly

I have a process that needs root privileges when run by a normal user. Apparently I can use the "setuid bit" to accomplish this. What is the proper way of doing this on a POSIX system? Also, how ...
goldilocks's user avatar
  • 88.1k
28 votes
1 answer
9k views

How to implement a horizontal cat? [duplicate]

Standard cat concatenates files line by line (row by row, if you will). I find myself needing a horizontal cat command more and more often recently; i.e. a command that takes a list of files and ...
Konrad Rudolph's user avatar
23 votes
8 answers
45k views

History command inside bash script

History is a shell-built in command I couldn't able to use that within a BASH script. So, Is there a way attain this using BASH script ? Here we go my script for you: #!/bin/bash history | tail -100 ...
Vijay Ramachandran's user avatar
10 votes
4 answers
32k views

Print a string including single quotes and other special characters

How can I write this with an AIX script correctly? My requirement is to write this command in test.txt: clock=$(prtconf -s | awk '{print $4,$5}') I tried this command: print 'clock=$(prtconf -s | ...
batil's user avatar
  • 311
4 votes
3 answers
18k views

Run a GUI program at startup

I am using Ubuntu 14.04. I wrote a small script named trial. The contents of the script are as follows: #!/bin/sh SHELL=/bin/sh PATH=/bin:/sbin:/usr/bin:/usr/sbin sh -c firefox I copied the ...
Joker's user avatar
  • 161
174 votes
4 answers
302k views

How can I execute local script on remote machine and include arguments?

I have written a script that runs fine when executed locally: ./sysMole -time Aug 18 18 The arguments "-time", "Aug", "18", and "18" are successfully passed on to the script. Now, this script is ...
AllenD's user avatar
  • 2,467
64 votes
7 answers
187k views

Execute remote commands, completely detaching from the ssh connection

I have 2 computers, localpc and remoteserver. I need localpc to execute some commands on remoteserver. One of the things it needs to do is start a backup script that runs for a number of hours. I ...
LVLAaron's user avatar
  • 1,755
1 vote
1 answer
2k views

Why crontab doesn't execute a scheduled bash script?

I have seen a lot of times (1, 2 )this question, but I didn't find the answer, so here I go. I'm running Debian 11, but I guess it works the same for any Debian-like distro. My crontab -e looks like: ....
k.Cyborg's user avatar
  • 507
24 votes
3 answers
82k views

Execute a command on user logon

In Linux desktop system, I want to execute a command when the user logs in. After reading some other posts, I tried to insert the command in ~/.bashrc but unsuccessfully. Moreover, the system uses a ...
BowPark's user avatar
  • 4,955
18 votes
8 answers
39k views

Add something to crontab programmatically (over ssh)

I have a deployment script.  It must add something to a user crontab (trigger a script that cleans logs every XXX days); however, this must be done only during the first deployment, or when it needs ...
sliders_alpha's user avatar
7 votes
1 answer
3k views

Parallel execution of bash script

I have a table URL_Experiment in my database (mySQL database). I have 2 million URL links in this table. For each URL, I am checking if some particular text is present in the URL and updating the ...
Ramesh's user avatar
  • 39.7k
6 votes
2 answers
2k views

How does bash interpret the equal operator with no surrounding spaces in a conditional?

The following script does not behave as I would have expected. Adding spaces around the '=' in the conditional made it perform how I wanted, but it got me thinking, what is it actually doing inside ...
Cory Klein's user avatar
3 votes
1 answer
2k views

Integration of afuse user-level automounter with sshfs as a systemd service spawned from login for local users

Edit 2020: I've been using the systemd-answer posted below for several years now, and am quite happy with it. To facilitate working with remote files, I setup afuse with sshfs to run from .bashrc of ...
Alex Stragies's user avatar
1 vote
1 answer
1k views

$! not being set to the PID of a > >(...) process substitution used with an external command

Bash 4.4.19(1)-release I have below a simple script which is the basis for a logging app. For various reasons I had to use process substitution. The runner is the heart of the app and since ...
conanDrum's user avatar
  • 455
135 votes
6 answers
489k views

Confusing use of && and || operators

I was skimming through an /etc/rc.d/init.d/sendmail file (I know this is hardly ever used, but I'm studying for an exam), and I've become a bit confused about the && and the || operators. I'...
josh-cain's user avatar
  • 1,841
71 votes
12 answers
118k views

Is there a unix command that gives the minimum/maximum of two numbers?

I was looking for a command to limit numbers read in from stdin. I wrote a little script for that purpose (critique is welcome), but I was wondering if there was not a standard command for this, ...
Minix's user avatar
  • 5,915
20 votes
3 answers
67k views

Bash: calculate the time elapsed between two timestamps

I have written a script that notifies me when a value is not within a given range. All values "out of range" are logged in a set of per day files. Every line is timestamped in a proprietary reverse ...
Marco's user avatar
  • 438
20 votes
11 answers
41k views

Extracting nested zip files

I have numerous zip archives, each of which contains a number of zip archives. What is the best way to recursively extract all files contained within this zip archive and its child zip archives, that ...
oadams's user avatar
  • 2,335
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,314
12 votes
5 answers
22k views

How do you colorize only some keywords for a bash script?

I am running some unit test code. The unit test code outputs regular text. There is a lot of the text so I want to highlight for the user important keywords. In this case the keywords are "PASS" and "...
Trevor Boyd Smith's user avatar
7 votes
5 answers
19k views

Bash script: check if a file is a text file [closed]

I am writing a menu based bash script, one of the menu options is to send an email with a text file attachment. I am having trouble with checking if my file is a text file. Here is what I have: ...
Powea's user avatar
  • 73
4 votes
4 answers
6k views

passing passwords to a script

I want to scp a file to a remote host, and from there, run a command on the file, and exit the remote session. If I write a usual script, call scp and then ssh, I am prompted to enter two passwords. ...
eli's user avatar
  • 303
4 votes
2 answers
2k views

Commands for determining level of usage of server

I am scripting a new program that will connect to a cluster of machines in a VM park, and hopefully check their level of usage. The VM clients are a combination of RHEL and CentOS, and runs on ...
larhauga's user avatar
0 votes
1 answer
2k views

/bin/sh: ./check-dependencies.pl: not found — but check-dependencies.pl exists!

I'm trying to build Moses, but when I run make I get the following output: minakshi@minakshi-Vostro-3500:~/Desktop/working-dir/moses/scripts$ make release # Compile the parts make all make[1]: ...
jenn's user avatar
  • 21
345 votes
5 answers
1.3m views

How can I get the current working directory? [duplicate]

I want to have a script that takes the current working directory to a variable. The section that needs the directory is like this dir = pwd. It just prints pwd how do I get the current working ...
user avatar
60 votes
7 answers
131k views

File extensions for unix shell scripts [closed]

On wikipedia, the article for .sh says: For the .sh file extension type, see Bourne shell. How about other unix shells? I know that the shebang is used inside the file to indicate an ...
Amelio Vazquez-Reina's user avatar

1
2 3 4 5
11