Questions tagged [shell-script]

Questions about shell scripts, executable files that are interpreted by a shell (bash, zsh, etc.).

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

init.d script not being run on boot

My script is not running on boot in a vagrant box under Ubuntu. My script looks like this - #!/bin/bash # /etc/init.d/mailcatcher ### BEGIN INIT INFO # Provides: scriptname # Required-Start: $...
4
votes
5answers
7k views

Batch rename files to a sequential numbering

I am trying to batch-rename a bunch of files in my shell, and even though there is plenty of material about it on the internet, I cannot seem to find a solution for my specific case. I have a bunch ...
144
votes
4answers
344k views

redirecting to /dev/null

I'm reading an example bash shell script: #!/bin/bash # This script makes a backup of my home directory. cd /home # This creates the archive tar cf /var/tmp/home_franky.tar franky > /dev/null 2&...
116
votes
3answers
201k 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 ...
72
votes
3answers
24k views

How can I test for POSIX compliance of shell scripts?

Considering that POSIX is the closest thing to a common standard among all unices, I'm interested in knowing if there's a shell that supports it exclusively. While most modern shells provide support ...
81
votes
9answers
34k views

How to do nothing forever in an elegant way?

I have a program which produces useful information on stdout but also reads from stdin. I want to redirect its standard output to a file without providing anything on standard input. So far, so good: ...
64
votes
8answers
54k views

Can a script be executable but not readable?

Is it possible to execute a script if there is no permission to read it? In root mode, I made a script and I want the other user to execute this script but not read it. I did chmod to forbid read and ...
49
votes
4answers
6k views

Should I care about unnecessary cats?

A lot of command-line utilities can take their input either from a pipe or as a filename argument. For long shell scripts, I find starting the chain off with a cat makes it more readable, especially ...
44
votes
7answers
10k views

What purpose does the colon builtin serve?

I've hacked on a lot of shell scripts, and sometimes the simplest things baffle me. Today I ran across a script that made extensive use of the : (colon) bash builtin. The documenation seems simple ...
65
votes
4answers
56k views

In bash, is it possible to use an integer variable in the loop control of a for loop?

I have the following bash script: #!/bin/bash upperlim=10 for i in {0..10} do echo $i done for i in {0..$upperlim} do echo $i done The first for loop (without the variable upperlim in the loop ...
42
votes
6answers
129k views

How to pass each line of a text file as an argument to a command?

I'm looking to write a script that takes a .txt filename as an argument, reads the file line by line, and passes each line to a command. For example, it runs command --option "LINE 1", then command --...
25
votes
5answers
24k views

“Friendly” terminal color names in shell scripts?

I'm aware of libraries in languages such as Ruby and Javascript to make colorizing your terminal scripts easier by using color names like "red". But is there something like this for shell scripts in ...
14
votes
2answers
5k views

How can I safely create and access temp files from shell scripts?

I've read that redirecting output to a fixed name file in /tmp can be a security risk, because if an attacker (or malcontent) notices that a file /tmp/tmpfileformyscript.tmp is created when I run my ...
20
votes
7answers
20k views

bash if not multiple conditions without subshell?

I want to combine multiple conditions in a shell if statement, and negate the combination. I have the following working code for a simple combination of conditions: if [ -f file1 ] && [ -f ...
8
votes
1answer
6k 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 ...
8
votes
3answers
33k views

Passing arguments from a file to a bash script

I've got this situation: ./ ./myscript.sh ./arguments.txt ./test.sh Inside myscript.sh, I have to run the file test.sh, passing to it the arguments contained inside arguments.txt. myscript.sh is: ...
20
votes
1answer
1k views

What does POSIX require for quoted here documents inside command substitution?

In this question someone reports a problem using a here document with a quoted delimiter word inside $(...) command substitution, where a backslash \ at the end of a line inside the document triggers ...
13
votes
9answers
2k views

Command line tool to “cat” pairwise expansion of all rows in a file

Suppose I have a file (call it sample.txt) that looks like this: Row1,10 Row2,20 Row3,30 Row4,40 I want to be able to work on a stream from this file that is essentially the pairwise combination of ...
92
votes
6answers
219k views

How do I delete the first n lines of an ascii file using shell commands?

I have multiple files that contain ascii text information in the first 5-10 lines, followed by well-tabulated matrix information. In a shell script, I want to remove these first few lines of text so ...
41
votes
4answers
26k views

What is the difference between running “bash script.sh” and “./script.sh”?

If script.sh is just something typical like #!/bin/bash echo "Hello World!" Is there a preferred way to run the script? I think you first have to chmod it so it becomes executable?
27
votes
4answers
58k views

How to inject keystrokes via a shell script?

I'm trying to inject keystrokes into the input daemon so as to simulate typing from a Bash script. Is this possible, and if so, how can I make it happen?
47
votes
4answers
30k views

Why does ( exit 1 ) not exit the script?

I have a script, that does not exit when I want it to. An example script with the same error is: #!/bin/bash function bla() { return 1 } bla || ( echo '1' ; exit 1 ) echo '2' I would assume ...
54
votes
6answers
71k views

Best way to follow a log and execute a command when some text appears in the log

I have a server log that outputs a specific line of text into its log file when the server is up. I want to execute a command once the server is up, and hence do something like the following: tail -f ...
51
votes
5answers
64k views

Determining if a file is a hard link or symbolic link?

I'm creating a shell script that would take a filename/path to a file and determine if the file is a symbolic link or a hard link. The only thing is, I don't know how to see if they are a hard link. ...
31
votes
5answers
35k views

getopt, getopts or manual parsing - what to use when I want to support both short and long options?

Currently I'm writing a Bash script which has the following requirements: it should run on a wide variety of Unix/Linux platforms it should support both short and (GNU) long options I know that ...
25
votes
4answers
20k views

Executing user defined function in a find -exec call

I'm on Solaris 10 and I have tested the following with ksh (88), bash (3.00) and zsh (4.2.1). The following code doesn't yield any result: function foo { echo "Hello World" } find somedir -exec ...
21
votes
4answers
4k views

keep duplicates out of $PATH on source [duplicate]

I have the following code that's source-d by my .shellrc PATH="${PATH}:${HOME}/perl5/bin" PATH="${PATH}:${HOME}/.bin" export PATH but if I make changes to other code and then source this file, my ...
15
votes
1answer
10k views

passing and setting variables in a heredoc

I have a script that has to do many different things on many different remote machines. I thought that a heredoc would work for this, but I am not able to use a variable defined elsewhere in the ...
6
votes
5answers
21k views

Stripping directory paths to get file names

What I am doing is converting mp3's with LAME. I already have an old script I wrote that works but I want to add to it this ability--to no longer delete the file but instead save it in a new root ...
16
votes
5answers
1k views

Stateful bash function

I’d like to implement a function in Bash which increases (and returns) a count with every call. Unfortunately this seems non-trivial since I’m invoking the function inside a subshell and it ...
11
votes
6answers
26k views

Why am I getting “line 1: $' :\r': command not found”?

I used Cygwin on my laptop (DOS). I have a collection of scripts from my colleagues, and my own. I am not an IT person, not knowledgeable in Unix. I follow my colleagues' syntax and able to manage a ...
4
votes
2answers
4k views

Shell script works when saved with nano but not when saved with Notepad++

When I copy my bash script across from Notepad++ into a new file with nano editor inside SSH and save. It runs fine. (sh ./install). But if I save the file (Exact same contents), upload to my web ...
7
votes
1answer
2k 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 ...
3
votes
3answers
2k views

command not found and syntax error near unexpected token

I have this script to divide a file over a 100000 or even more in 50000 lines. desc() { echo $...
67
votes
9answers
38k views

Split pages in pdf

I have a scanned pdf file which has scanned two pages on one virtual page (page in pdf file). The resolution is with good quality. The problem is I have to zoom when reading and drag from left to the ...
59
votes
12answers
21k views

Why write an entire bash script in functions?

At work, I write bash scripts frequently. My supervisor has suggested that the entire script be broken into functions, similar to the following example: #!/bin/bash # Configure variables ...
81
votes
3answers
48k 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" ...
46
votes
13answers
7k views

What is a fast command line way to switch between multiple directories for system administration?

What is a fast command line way to switch between multiple directories for system administration? I mean, I can use pushd . and popd to toggle, but what if I want to store multiples and cycle through ...
62
votes
5answers
30k views

Can't indent heredoc to match nesting's indent

If there's a "First World Problems" for scripting, this would be it. I have the following code in a script I'm updating: if [ $diffLines -eq 1 ]; then dateLastChanged=$(stat --format '%y' /....
35
votes
5answers
102k views

How to introduce timeout for shell scripting?

I want to run a shell script that got a loop in it and it can go for ever which I do not want to happen. So I need to introduce a timeout for the whole script. How can I introduce a timeout for the ...
20
votes
12answers
55k views

How can I remove all comments from a file?

I have a file with comments: foo bar stuff #Do not show this... morestuff evenmorestuff#Or this I just want to print all the uncommented code: foo bar stuff morestuff evenmorestuff Being able to ...
26
votes
5answers
83k views

How to read from two input files using while loop

I wanted to know if there is any way of reading from two input files in a nested while loop one line at a time. For example, lets say I have two files FileA and FileB. FileA: [jaypal:~/Temp] cat ...
24
votes
8answers
100k views

How do I append text to the beginning and end of multiple text files in Bash?

I have a directory full of text files. My goal is to append text to the beginning and end of all of them. The text that goes at the beginning and end is the same for each file. Based on code I got ...
41
votes
4answers
46k views

Finding largest file recursively

I am trying to find the largest file in a directory recursively. If there is a subdirectory inside of that directory the function needs to go inside that directory and check to see if the largest file ...
23
votes
7answers
68k views

Text between two tags

I want to retrieve whatever is between these two tags – <tr> </tr> – from an html doc. Now I don't have any specific html requirements that would warrant for an html parser. I just plain ...
36
votes
4answers
57k views

Why doesn't “sudo su” in a shell script run the rest of the script as root?

A sample script can be as below: #!/bin/bash sudo su ls /root When using ./test.sh as the normal user, instead run ls as super user and exit, it switches to root; and when I logout, it executes ls /...
15
votes
9answers
39k views

Count files in a directory by extension

For the purpose of testing, I'd like count how many images files are inside a directory, separating each image file type by file extension (jpg="yes". This because later it will be useful for another ...
11
votes
5answers
45k views

move file by list in file (with leading whitespace)

I have a file that contains file names. For example: /tmp/list.txt (it is with the spaces at the start of each line): /tmp/file.log /app/nir/home.txt /etc/config.cust I want, using one line, ...
17
votes
4answers
19k views

dirname and basename vs parameter expansion

Is there any objective reason to prefer one form to the other? Performance, reliability, portability? filename=/some/long/path/to/a_file parentdir_v1="${filename%/*}" parentdir_v2="$(dirname "$...
20
votes
7answers
66k views

Binary to hexadecimal and decimal in a shell script

I have a context where I need to convert binary to hexadecimal and decimal and viceversa in a shell script. Can someone suggest me a tool for this?