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

learn more… | top users | synonyms (2)

2
votes
1answer
81 views

See if a script is running when using #!/usr/bin/env

I have tried migrating the shebang for my bash scripts from #!/bin/bash to #!/usr/bin/env bash, and some of them were broken because they relied on this code that checks for existing instances of ...
2
votes
2answers
64 views

How to duplicate a file a number of times while embedding an index in each file

I have a file named, for example, ascdrgi.txt, with the following contents: tiger lion cat I want to duplicate this file a (variable) number of times by changing the last character of the filename ...
2
votes
1answer
31 views

Bash scripting to process an experiment data file

I have a .txt file with a sample snapshot of the content shown below. I have to sum all the field values and output them. That is, sum total CEPSDIST (MEAN), sum total SRMR, etc. I want to use bash ...
2
votes
1answer
95 views

how to force shell script characters encoding from within the script

I have a few shell scripts with UTF8-encoded characters inside and i want to be sure that they are decoded correctly regardless of the machine locale settings. Is it possible to force the shell (bash ...
2
votes
2answers
43 views

Check Input Prior to Executing a “for loop”

is there any way to check or test the input from a 'read' against a for 'loop'? I don't want the case statement to execute there any of the input is incorrect. As of right now it will loop through, ...
2
votes
2answers
129 views

How to find unique word in a single line

I have a string which is something like 50003 50003 50003 50001 this I get after processing into another file which is stored into a variable. now i want from that variable unique words. Like 50003 ...
2
votes
1answer
19 views

Parameter ignored when quoted

I have a directory with 3 files: file1, file2 and the following bash script: #!/bin/bash set -e Command="ls -1 -I file1" echo "Command: $Command" Files=`$Command` echo "Files:" echo $Files The ...
2
votes
1answer
41 views

who to get a count of “base subDir” without there subDirs?

All I am trying to do is get a count of direcrories on one leve of a tree. so Far I have this. find /media/data/temp1 -type d -exec basename {} \; which gives me this: temp1 Coldplay Greatest ...
2
votes
2answers
120 views

Avoid output buffering for a command group (curly braces) in bash script

I have a bash script containing a group of commands in curly braces { ... }. This group contains some initial echo commands and then one loop. At each iteration the loop executes various slow commands ...
2
votes
2answers
251 views

Curl url txt file, but grep each url seperately from single file

Ok, here's one for you guys that is puzzling me. So I have a text file with lots of url's in it. I'm using curl -K "$urls" > $output to spit the output to my output file. Now for the output of ...
2
votes
1answer
58 views

What purpose does reassigning a variable to itself via echo serve?

I ran across some bash code during a review that reassigns a variable to itself using echo. The variable is an array. The code basically assigns the variable to itself with `echo $a`. a=(one two ...
2
votes
2answers
468 views

How to make bash built-in “read” ignore commented or empty lines?

(For simplicity I'll assume the file to read is the first argument - $1.) I can do what I want externally with: tempfile=$(mktemp) awk '/^#/ {next}; NF == 0 {next}; {print}' "$1" > $tempfile ...
2
votes
1answer
78 views

How do I run a script every time “yum update” is run?

I'd like to run a script (let's say myScript.sh) every time a yum update or yum install command is executed. So if I run yum update someprogram then myScript.sh should be executed right after that. ...
2
votes
2answers
46 views

alphabetical expansion order for *

When I'm using a POSIX compliant shell (es: dash, bash, zsh, ...) can I be sure that * will always expand in alphabetical order (dictated by LC_COLLATE)? example: $ echo 1 > file_a $ echo 2 > ...
2
votes
3answers
46 views

How do you append to a variable string in a script?

How do I append a string that has variables in it? sed 's/MIMG109AL1ARP\t[[:alnum:]]*\t/ /g' I initially thought sed, but I don't want to replace anything, I want to add something after the intial ...
2
votes
1answer
84 views

Error when substituting part of curl command with variable

I'm writing a script where I am running curl commands several times. The use of the curl command varies throughout the script and I would like to save myself time (in the future when I have to debug ...
2
votes
1answer
80 views

Is it possible somehow to tell if a laptop is moved in a bash or python script?

I'm trying to figure out if there's a way to use a bash or python script to tell if my laptop gets moved. I had the idea of using wifi signal strength to do it, but the problem with that is that you ...
2
votes
1answer
55 views

How to enter a sum derived from awk in the middle of text output [closed]

I am trying to print a sum in the middle of a line. My script prompts for a user name and then pulls a total number of processes and a total of time for that user from a file. I want to print a ...
2
votes
2answers
94 views

Help with script reading numbers from a file and determining if they are even or odd numbers

So I want my script to test for the existence of a file which I already populated with numbers. It does that, but after it does that I want it to run a while look checking to see if the numbers in the ...
2
votes
2answers
44 views

How do I write the output of Portage to a txt file?

I would like to be able to write the output of Portage commands, along with other commands, that I perform in tty (that is, the screen-wide terminals started with Ctrl+Alt+Fn where n represents an ...
2
votes
2answers
121 views

Conditional execution block with || and parentheses problem

So this should be simple. I am trying to test a condition at the top of a script and bail out of the entire script when the condition fails, and I have two statements I want to execute when I bail. ...
2
votes
2answers
129 views

Works on command line but not in shell script

I have a file formated like below: 436541,000454056,Smith,john,jsmith,j-Smith@xxx.com I want to extract out ",jsmith" and write it to another file "test.txt". So I use: grep -o ...
2
votes
3answers
46 views

need to move files with name into directories with corresponding names

I have files named patient_sample_L001_R1_001, patient_sample_L002_R1_001, patient_sample_L003_R1_001, patient_sample_L004_R1_001 another set named patient_sample_L001_R1_002, ...
2
votes
1answer
109 views

How can i order the output of “echo” command

I have created one script with shell scripting and the output of the script is : but i want to be the output is like this : and the code is : if [ $CS == 0 ]; then printf $BLUE echo "$url$i ...
2
votes
2answers
55 views

Write permissions inside shell script

How to check user has write permission to the directory,the directory name is passed as a command line argument.
2
votes
1answer
53 views

Shell Script - Redirected output to a file and results are not as expected in first run

I have redirected the output of the command which returns some times 2 rows, 5 rows.. 100 rows etc in the file.. and i will use this file in if condition later to check some other conditions.. ...
2
votes
1answer
34 views

Exiting out of a script that restarts application

I have a bash shell script that uses wait() to determine if an application exited. However, I am having a hard time killing the script/application when I want it to. The idea of the script is to ...
2
votes
1answer
113 views

Background vs foreground task and running tasks in parallel

Since I'm new to Linux, apologies if I'm not using the correct terminology. I'm trying to run a number of tasks in parallel (using the background shell operator). However, I need to first run two ...
2
votes
1answer
126 views

Is it possible to start a command from bash and send a keystroke to it

I'm trying to start Firefox from bash and I want it to open the webconsole on start (this can be done by pressing F12 rigth after it started). Is there a bash command that starts the programm and ...
2
votes
2answers
132 views

How to convert several tiff images into xyz coordinates in a single text file using the terminal?

I have limited experience with programming and Linux and I need your help. The situation I am in is that I have a stack of black and white .tiff images (approx. 400 of 10 Mb each one) which I need to ...
2
votes
2answers
23 views

Sub Shell Redirection Error With Variable ( while … ) $3 > $testdir/$testfile.log

My scripts executes the sub shell command along the lines of: ( while ..... ) $3>$testdir/$testfile.log I get the error: line 75: syntax error near unexpected token `$3' line 75: ` ) ...
2
votes
1answer
257 views

Huge delay in script response when using SFTP with expect

I am using a script to call SFTP service to fetch some logs from the remote server. This works fine in normal scenario but, i have noticed a very long delay of around 30 minutes or more in some of the ...
2
votes
2answers
98 views

How can I list files by class/indicator (as in ls --classify) then by name?

ls --clasify append indicator (one of */=>@|) to entries. I would like to group the entries of the same class/indicator together. I can only find ls --group-directories-first for grouping the / ...
2
votes
2answers
207 views

is a script a program being run under the current shell or a child shell?

I am confused with regards to the following - if I execute a script or run a program in the current shell by typing on its name it searches in the $PATH variable, to find the location of the ...
2
votes
2answers
679 views

Merge two-columns files into one file

I have a bunch of files (file1, file2,file3, ...) with two columns. For example, file1 looks like: 0.12 0 0.32 0 0.42 1 0.23 0 and file2 looks like: 0.34 1 0.55 1 0.31 1 0.99 0 I wonder how to ...
2
votes
2answers
903 views

Call a script for another script, but don't exit the parent if the child calls exit

first.sh: #! /bin/ksh echo "prova" . ./second.sh echo "ho lanciato il secondo" . ./third.sh echo "ho lanciato il terzo" second.sh: echo "sono nel secondo script" dosomething1 exit $? If ...
2
votes
1answer
92 views

Stable timing for shell scripts?

The run times of scripts vary quite much. I would like to find similar time -function as Matlab's timeit, described here. For instance, I am evaluating these commands here about counting quickly ...
2
votes
2answers
251 views

How to make this `echo | bc` output be on the same line as the echo output after?

I'm making a program for generating random numbers, and before it generates, I want to show how many combinations there are. When I do it, I get this: 64k@conroe$ bash random A-Za-z0-9 3 238328 ...
2
votes
1answer
174 views

Capture specific column from command output

I know how to redirect first 5 lines of the output to a file when I execute a command. I want to know how to redirect only specific information from the output. For example I would like to execute ...
2
votes
2answers
238 views

Copy certain files from specified subdirectories into a separate subdirectory

I want to copy all .fastq files from every subdirectory that ends in '_CSF' into another already created subdirectory, dir1_CSF. I understand how to copy certain files but not from certain ...
2
votes
1answer
747 views

For Loop for Google Image Downloading Bash Script

I have a bash script which downloads images from Google Images. This is the command I use: bash getimages.sh 3 rocky%20mountains That command will download the 3rd picture in Google Images. To ...
2
votes
4answers
313 views

Extract two values from the output of a command

I'm using the following command in a script to get synthetic wifi info: echo "$( iw dev wlp1s0 link | grep '^\s*SSID:\s' | sed -r 's/^\s*SSID:\s//' ) $( iw dev wlp1s0 link | grep ...
2
votes
1answer
211 views

Writing a tab completion script for bash

I made a shell script that reads input from user and kills a process. But in bash if the user presses Tab key then bash suggests some keyword or file names. My script however doesn't suggest anything. ...
2
votes
1answer
109 views

How create a Init Script LSB Header for this script in Debian?

I have this script below, but how can I create an LSB Header for it in Debian? I want to put in my init.d. I know that for Fedora is ok, but for Debian I don't know. Or where and how I put in ...
2
votes
1answer
52 views

Clustering identical files ignoring spaces & linebreaks

Consider a folder with many XML files (10K small text files). Some XML files are identical, some are different. I would like to find out what files are identical (ignoring whitespaces, tabs and ...
2
votes
1answer
236 views

Passing variables to range patterns in awk

I'm trying to use range patterns in awk: grep -a volume somefile | awk '/^Apr 25 23:44:04*/,/^Apr 26 12:44:01*/ {print}' This works fine, but when I try to execute it by putting the date and time ...
2
votes
1answer
5k views

How to run a shell script (bash) on a chromebook?

I have a shell script and I need to run it in order to connect to a remote ubuntu linux machine having encrypted HDD, in order to enter the passphrase. There's a shell script for this, but I have a ...
2
votes
1answer
58 views

How to download a lot of documents on the terminal from a server?

I have to write a bash script (.sh) with less than 300 characters which downloads all of the genomes 22 in this server ftp://ftp.ncbi.nih.gov/genomes/Homo_sapiens/ (the ones with fa.gz extention) and ...
2
votes
3answers
245 views

Command line options with argument in shell script

I have a script to get ldap user name, email and mobile number: #!/bin/bash echo -n "Enter Unix id > " read UNIXID ldapsearch -x "(cn=$UNIXID)" | awk '/givenName/||/mobile/||/mail/' Here is the ...
2
votes
1answer
163 views

ksh/bash Formatting Files through sed by cat file|sed command

I am new to unix platform, (Alternate Approaches can be discussed) I am trying to change the colors of content of a file conditionally. My file is: A B C D Value Value Value Value Value ...