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
9
votes
3answers
3k views

Mass .flac --> .mp3 transcoding: How to write a shell script that preserves ID3 tag information?

In recent weeks I've gone from a fairly 'hands-on' approach to .flac --> .mp3 transcoding, to one that's far more 'set & forget'. The first step was to stop using a GUI front end (Audacity with a ...
9
votes
3answers
21k views

Editing config file via a bash script

I've been trying to write a simple bash script which I'll be using to install an application and update it's config file. I'm having hard time to have get it's config file modified. # DBHost=...
8
votes
4answers
5k views

Flattening folder structure

I have this folder structure: ├── foo1 │   ├── bar1.txt │   └── bar2.txt ├── foo2 │   ├── bar3.txt │   └── bar4 with a space.txt └── foo3 └── qux1 ├── bar5.txt └── bar6.txt that ...
8
votes
3answers
2k views

Measuring time within a script

This thread shows how to measure the time it takes to run a script. In my case, I am interested in measuring time between two points within a script. Here is an example of how I would like to use ...
6
votes
2answers
17k views

Using Diff on a specific column in a file

Will it be possible to use diff on a specific columns in a file? file1 Something 123 item1 Something 456 item2 Something 768 item3 Something 353 item4 file2 Another 123 stuff1 Another 193 ...
6
votes
3answers
14k views

Formatted shell script output

How can I fix the output width using printf? This is an example script: #!/bin/bash OK=$(printf '\t%+50s\n' OK) FAIL=$(printf '\t%+50s\n' FAIL) for i in a aa aaa aaaa aaaaaa aaaaaaaaaaaaaaaaa ...
6
votes
4answers
39k views

“Can't cd to /home/user” when sourcing a script [duplicate]

I have this script: echo $HOME cd $HOME cd / cd /usr/local/src/ When I run it like this . script.sh I get this output: /home/user : No such file or directory : No such file or directory : No such ...
5
votes
5answers
23k views

Extracting IP address from a text and store it in a variable

I have a text file named abd shown below. 48878 128.206.6.136 34782 128.206.6.137 12817 23.234.22.106 I want to extract only IP address from the text and store it in a variable and use for other ...
4
votes
1answer
1k views

Why does the history command do nothing in a script file?

If I create a file containing #!/bin/bash history and run it, I get no output. Running it manually in a terminal or sourceing the script file does produce output. Why does history not work from ...
2
votes
2answers
5k views

Sort files according to their extensions

I have made a script that will sort files according to their extension and place them in the proper folder. For example, place abc.jpg in the directory jpg. #!/bin/bash #this script sorts files ...
0
votes
2answers
8k views

Shell Script: Sending input to background process

I am using the following script to watch a folder, and if the size changes and a file doesn't exist, it runs a script. #!/usr/bin/sh SCRIPT="dim_import/xdir" while true; do size=$(du -s ...
22
votes
3answers
18k views

How to list files and directories with directories first

I have two questions. First, which command lists files and directories, but lists directories first? Second question: I want to copy a list of files into a single directory, but make the target ...
16
votes
4answers
13k views

Building paths robustly

Say I have several variables in a shell script (e.g. in zsh): FOLDER_1, FOLDER_2, etc. These variables refer to folders descending from /. For example, if I have a path /home/me/stuff/items the ...
15
votes
5answers
22k views

How to parse ISO8601 dates with linux date command

I'm trying to use the date command to generate a file timestamp that the date command itself can interpret. However, the date command does not seem to like its own output, and I am not sure how to ...
14
votes
3answers
39k views

How to take 'password' like input in shell script? [duplicate]

Possible Duplicate: How to hide commands typed in a Linux shell? Reading passwords without showing on screen in Bash Scripts I would like to take a password as input, but I want user to be able ...
11
votes
1answer
203 views

How can I test my shell script's file-handling robustness?

I've written a shell script that handles some "regular" filenames, but I've read Why does my shell script choke on whitespace or other special characters? and Why you shouldn't parse the output of ls ...
10
votes
4answers
18k views

Bash, execute command after invoking a new shell

I am trying to make a script like this: #!/bin/bash sudo -s something... When I execute it, I get a new shell but something is executed only when I exit the shell created by sudo -s, not inside it. ...
10
votes
4answers
1k views

What is the easiest way to execute text from tail at the command line?

Sometimes I'm working on a new (ubuntu) box and I type git and am alerted: The program 'git' is currently not installed. You can install it by typing: apt-get install git-core If that happens I ...
9
votes
5answers
13k views

how to check directory is empty

I have a requirement, if I execute a script ./123 with an arguments of empty path, say /usr/share/linux-headers-3.16.0-34-generic/.tmp_versions(this directory is empty). It should display "directory ...
9
votes
4answers
18k views

How can I assign an initial/default password to a user in Linux?

I found a guide that explains how to set a user's password. I'm trying to automate it and send an e-mail to the user like: userid created with password XYZ. request to change the initial password. ...
9
votes
3answers
3k 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 ...
8
votes
2answers
30k views

Why does the filename contain question marks when I run a script on my remote machine?

I have a tiny script that simply fetches the current date, runs a PHP script and redirect the output (and errors) to a filename containing the current date. DATE=$(date +"%Y%m%d") FILE="log/${DATE}....
7
votes
1answer
13k views

start autossh reverse tunnel automatically when network comes up

I connect from my home PC thru an Internet server I own to a restricted client (that I also own) using a reverse ssh tunnel. I have a script that sets up the restricted client to middleman leg of ...
7
votes
1answer
17k views

How to assign result of sed to variable

Below is a complete copy of a demo I'm using to to figure out to get the sed command to get rid of the spaces in a persons name, and compress it down to not have spaces. Once this is done, I want to ...
6
votes
5answers
13k views

Bash: Parse multi-line into single-line commands

I have to write a Bash script which checks if another Bash script contains a certain command line. Since Bash allows you to split a command line over multiple lines, my script must be able to merge ...
6
votes
3answers
1k views

How do I prevent a script from terminating when the shell exits?

In particular, I created a script to start Firefox which I double-click and choose 'Run in terminal,' but when the shell exits Firefox is killed immediately. How do I prevent this from happening?
6
votes
3answers
2k views

How to check if a glob has an expansion? [duplicate]

Is there a good clean conventional way to do this? For example, if there are any ".gz" files in a certain directory, I want to unzip them. But if there aren't, I don't want to see any error. If I ...
6
votes
7answers
140k views

Upload file to ftp server using commands in shell script? [closed]

I want to upload a file from my Linux server to some FTP server. I know we use put and get commands to upload and download files and the sftp command to connect to the FTP server. But I wanted to do ...
6
votes
1answer
2k views

Code works manually on terminal but can't run script

I created this script to backup my postgresql database with cron: backup.sh #!/bin/bash export PGUSER="user" export PGPASSWORD="pass" FECHA_ACTUAL=`date +%Y-%m-%d` HORA_ACTUAL=`date +%H:%M` ...
5
votes
2answers
9k views

Concatenate multiple files under subdirectories

My directory structure is given below x:\Project_2012-158A\Sample_4041 SampleSheet.csv 4041_CGTACG_L002_R1_001.fastq 4041_CGTACG_L002_R2_001.fastq 4041_CGTACG_L006_R2_001.fastq ...
5
votes
3answers
14k views

How to delete old backups based on a date in file name?

I have a daily backups named like this: yyyymmddhhmm.zip // pattern 201503200100.zip // backup from 20. 3. 2015 1:00 I'm trying to create a script that deletes all backups older than 3 days. The ...
4
votes
1answer
3k views

gnuplot shell variable substitution and arrays

I need to use shell variables in my gnuplot commands, for which I'm using the here document style. I also need to use loops inside the gnuplot code. Both these things are working. Now -- I want to ...
4
votes
1answer
26k views

How to use variables in sed command

I'm trying to run sed -i "/$variable/c \$variable1' file.txt But it's not displaying any change in the file: file.txt. But the same command, with out the variables works fine: sed -i "/variable/c \...
4
votes
4answers
1k views

How to escape shell metacharacters automatically with `find` command?

I've got a bunch of XML files under a directory tree which I would like to move to corresponding folders with the same name within that same directory tree. Here is sample structure (in shell): ...
4
votes
1answer
1k views

mv : not found when executed from script that modifies PATH variable

I have this in my script. But is returning an error. If I execute the command in the console works fine ./script.sh[16]: mv: not found [No such file or directory] I want move some files what starts ...
2
votes
4answers
483 views

What exit modes exist in shell-scripting in general and in Bash in particular?

I know that in shell-scripting an "exit" usually means voluntarily or at least successfully terminating a session (or a process within a session) and that there are several different exit modes; the ...
2
votes
4answers
2k views

Modify all bash commands through a program before executing them

I am trying to create a program which require such feature. The flow will be like: User enter a bash command User hit the enter My script will get command, current directory,.. as variables. Program ...
0
votes
1answer
55 views

Shell script to display file systems which uses more than 90% disk usage

df -h | awk '{ print $5 " " $1 }' > sample.txt while read -r line; do echo $line var1 = $( echo $line | cut -d "%" -f5 ) if [[ ( var1 > 90 ) ]] then echo $line sample1.txt fi done < sample....
27
votes
5answers
5k views

Why do I need to place “do” in the same line as “for”?

1. Summary I don't understand, why do I need E010 bashate rule. 2. Details I use bashate for linting .sh files. E010 rule: do not on the same line as for for bashate: Correct: #!/bin/bash for ...
20
votes
4answers
3k views

Is there a “reverse sudo”? [duplicate]

Let's say I have a script that will be executed on various machines with root privileges, but I want to execute certain commands within that script without root. Is that possible?
14
votes
1answer
36k views

remove lines where a field's value is less than or equal to 3 - sed or awk?

I need to remove every line that has a value of 2 or less in the 8th field (column). My data looks like this: 12-31 Airport 189 379 41 49.70946503 -124.91377258 2 2880 30.8 01-01 ...
14
votes
4answers
9k 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?
13
votes
2answers
2k views

Why are quotes necessary for files argument when calling this Bash script?

I'm quite new to Bash scripting. I have a "testscript", which I used as the basis for a more advanced / useful script: #!/bin/bash files=$1 for a in $files do echo "$a" done When I call this ...
13
votes
2answers
7k views

Why is $1 in a function not printing the script's first argument?

Why doesn't echo $1 print $1 in this simple bash script? #!/bin/bash # function.sh print_something () { echo $1 } print_something $ ./function.sh 123 -> why doesn't it print '123' as a result?
12
votes
2answers
5k views

How to get block device name from partition device name?

I'm looking for a portable way to obtain parent block device name (e.g. /dev/sda) given the partition device name (e.g. /dev/sda1). I know I could just drop the last character, but that wouldn't work ...
11
votes
2answers
5k views

Bash variable substitution of variable followed by underscore

The variable BUILDNUMBER is set to value 230. I expect 230_ to be printed for the command echo $BUILDNUMBER_ but the output is empty as shown below. # echo $BUILDNUMBER_ # echo $BUILDNUMBER 230
9
votes
3answers
3k views

Change only the extension of a file [duplicate]

I am working on a simple shell script to change a file one.PDF to one.pdf. I have this file stored in a folder called Task1. My script is in the same directory as Task1. Called Shell1.sh When I run ...
8
votes
6answers
4k views

How to tweet using terminal?

I would like to tweet a message using terminal. I tried something like: curl -u 'TwitterUsername':'TwitterPassword' -d status=”Your Message Here” https://twitter.com/statuses/update.xml but seems ...
8
votes
5answers
12k views

How to get subshell's PID in Korn Shell (equivalent of $BASHPID)

In bash you have this handy variable: $BASHPID wich always returns the currently running subshell's PID. How can I get a subshell's PID in ksh? For example see the code below: #!/usr/bin/ksh93 echo "...
7
votes
3answers
4k views

Use bash's read builtin without a while loop

I'm used to bash's builtin read function in while loops, e.g.: echo "0 1 1 1 1 2 2 3" |\ while read A B; do echo $A + $B | bc; done I've been working on some make project, and ...