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

learn more… | top users | synonyms (2)

3
votes
2answers
72 views

terminal warn on close when opened within a script

First, the question: How can I warn the user that a script is running when the user clicks the close button and the terminal itself (and the process running in it) was started from the command line. ...
1
vote
1answer
88 views

awk in ssh in su in a command substitution

I am creating a script that will ssh to a host and print all the user accounts and when they will expire. On a host I can run awk -F':' '{ print $1}' /etc/passwd and it will give me a list of all ...
0
votes
1answer
84 views

create a user via script

I want to create a new user in a specific group when I issue the below command. sudo ./create_user.sh "test" "firstname lastname" The create_user.sh script looks as below. #!bin/bash #sudo ...
0
votes
1answer
57 views

Can't concat file paths, permission denied

I can't realize why if I just try to concat some file paths (without opening the files), I get "permission denied": #!/bin/bash if [ "$#" -eq 1 ]; then path1= "./src/$1" path2= ...
1
vote
2answers
66 views

How to make a filter more efficient

I've created a script to compare a list of users from a MSSQL database against a list of disabled AD users. The tool we use for AD authentication returns data in the following format (with plenty ...
1
vote
3answers
153 views

iterate over file and send lines as positional parameters to another file

I would like to iterate through a file containing dates (named dates.txt) in the following format: 2009 08 03 08 09 2009 08 03 09 10 2009 08 03 10 11 2009 08 03 11 12 2009 08 03 12 13 2009 08 03 13 ...
0
votes
0answers
41 views

open Mysql connection for long time to execute queries in block

I am writing one script which execute bunch of queries in block. here block means version number, each version have different different queries. I write two shell script. 1 version.sh version.sh ...
1
vote
3answers
122 views

Can I make [ $(($1 % $check)) -ne 0 ]; be more performant and not be killed?

If have the following code which works for up to 13 (line 2) in a few seconds but when I try and do upper teens and 20 it gets killed after a couple of minutes. divisible_by () { for ((check=2; ...
-2
votes
1answer
123 views

Why do I get “-eq: unary operator expected false”? [duplicate]

I get $ ./5_divisible_by_1_to_10.sh ./5_divisible_by_1_to_10.sh: line 16: [: -eq: unary operator expected false true for: divisible_by () { under_test=$1 from=2 to=4 divisible=0 for ...
2
votes
1answer
87 views

Avoid backslash expansion with echo in dash

First create a file with this exact content: a\nb I named this file foo, if I run the following, it prints the exact content of the file: bash -c 'bar=$(cat foo);echo "$bar"' But if you run it ...
4
votes
1answer
37 views

Simple problem, Long read: How to overwrite file residing on ESP at Linux boot

First, foundation. Here is my setup: I have a 64-bit dual-boot system running Windows 7 and Linux Mint 17 Cinnamon. I use rEFInd to graphically choose which system to boot. A little bit about ...
34
votes
2answers
4k views

Why is using && 75 times faster than if…fi and how to make code clearer

I have the following working code: largest_prime=1 for number_under_test in {1..100} do is_prime=true factors='' for ((divider = 2; divider < number_under_test-1; divider++)); do ...
1
vote
1answer
26 views

How to use passed-in parameters for shell script?

I know little about shell script but I want to try to implement some complicated scripts. Say I have a doitall.sh with the content like: zip -j version_a_en.zip en_filea1.txt en_fileb2.json And a ...
2
votes
1answer
72 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 ...
2
votes
1answer
139 views

Merge some tab-delimited files

I have 100 files with 57,816 rows each.I want to do an outer join of these files by merging on basis of the common column. I am using R programming for this but this is very slow. fileList <- ...
2
votes
3answers
97 views

How can I run Linux shell scripts which needs root privilege from a web page

I'm trying to run a linux shell script which requires root privilege via Apache, Is there any way to run scripts which needs root privilege, rather than setting suid and sudo privilege to Apache. ...
0
votes
1answer
48 views

For loop brackets - C like syntax

When i am using the below syntax to iterate why two brackets are needed ? for (( expr1; expr2; expr3 )) do command1 command2 .. done and the below code doesn't work ? and throws error "syntax ...
1
vote
1answer
44 views

Unix scripting - for do command with & [duplicate]

I am trying to use a for do command that I can run as a one liner in Solaris/Linux, and would like the do command to run asynchronously. I have tried the following: for i in `ls AMQ*.FMT`; do ...
-3
votes
2answers
94 views

Building tree diagram [closed]

Suppose I have a program P11 in a directory; it calls 2 other programs P21 & P22. Each of P21 & P22 call 2-2 other programs. i.e. Level-1: |--------------P11-------------| Level-2: ...
2
votes
1answer
135 views

What is the meaning of ##* in shell script? [duplicate]

Script: text="//ABC/REC/TLC/SC-prod/1f9/20/00000000957481f9-08d035805a5c94bf" echo ${text##*/} Here, what is the meaning of 2nd line in the above 2 line script?
0
votes
2answers
62 views

Combining two bash script and using a menu

I have two scripts, one that is used on a folder with csv files and another on a folder with .tar.gz files. In the second script, I first unzip the files before doing anything with them. Now I want to ...
0
votes
1answer
92 views

linux/shell command to control screen brightness in android

I am working on a kiosk project using an android tablet.If there is no input power to the kiosk for a long time then, the tablet will eventually shutdown.In order to auto-reboot the tab when the power ...
2
votes
3answers
53 views

Email using shell script using email address from external flat file

I'm using alexkowalski script from Email using shell script MAILADDR=(mail1@example.com, mail2@example.com, mail3@example.com) for i in "${MAILADDR[@]}" do echo "Mail test..." | mail -s ...
0
votes
1answer
37 views

A small script about wget

I want to download an image file called mch_aktuell_gross_20141229_79514688.gif (basically a forecast gif) from www.meteoradar.ch Here is my script: #!/bin/bash wget \ --recursive \ ...
5
votes
1answer
148 views

echo colored text which changes colors dynamically

Is there any way that i can print a colored text on console which keeps on changing its colors dynamically(with or without a specified time interval) just like some greeting cards in which the ...
2
votes
2answers
68 views

Find file, check size and overwrite when filesize is different

Today I found a little bug in my own written CMS. Now I want to rewrite all the files on the server with the same filename. However, I do have some customers with the specific file customized so I ...
6
votes
2answers
385 views

Move file but only if it's closed

I want to move large file created by external process as soon as it's closed. Is this test command correct? if lsof "/file/name" then # file is open, don't touch it! else if [ 1 -eq ...
2
votes
2answers
110 views

Create arrays with brace expansion in loop

I would like to generate a number of arrays that differ only by year. In the loop, I create the arrays with brace expansion and a variable. I tried the following code without success: ...
0
votes
1answer
70 views

need help with a a symlink and the PATH variable

I tried to install drush with composer from a script that is runing with sudo $nameofuser `logname` #drush installation getcomposer(){ # Composer Global installation curl -sS ...
2
votes
0answers
36 views

Generate array of strings combined with number

I would like to generate a local array in a shell/bash script that consists of the following elements: JF-1998, JF-1999, JF-2000,... , JF-2011. That is, a string JF- combined with the years from 1998 ...
1
vote
1answer
44 views

How to check the file type in a script

I want to do a loop for all the images in a directory. The images doesn't have extension so I have to read the first bytes of the image to know its type. The loop should end up being something like. ...
3
votes
0answers
37 views

Rename file with the name from a variable [duplicate]

I want to rename an image with the text in that image. For that I use an OCR and I try to store the name in a variable and use that variable in the mv command but I get mv: target ‘file’ is not a ...
3
votes
2answers
94 views

Append unix timestamp to files with space using a script

This script works fine for all files recursively as well but it does not name files that has space in between. For example it throws error for the files "this is a test.txt" or "test file.txt" Any ...
0
votes
1answer
26 views

how to export nam of logged in user correctly?

I have the following script here. #!/bin/bash #some initial variables DLURL=https://raw.github.com/horvan/drubuntu/master DIRURL=/opt/.drubuntu #Get files to run the installation wget -O ...
4
votes
5answers
405 views

What command(s) will feed a tab-delimited text file and cut each line to 80 characters?

I've got multiple-line text files of (sometimes) tab-delimited data. I'd like to output the file so I can glance over it - so I'd like to only see the first 80 characters of each line (I designed the ...
2
votes
2answers
67 views

Use config file for my shell script

I need to create a config file for my own script: here an example: script: #!/bin/bash source /home/myuser/test/config echo "Name=$nam" >&2 echo "Surname=$sur" >&2 Content of ...
1
vote
2answers
55 views

Creating a list of files, removing “duplicates” with different suffix

I have a problem that (I think) require a bit of script-magic - not sure what would be best though... I have one directory with lots pictures in different formats - jpg, gif, png, tiff and svg. Some ...
1
vote
3answers
57 views

cd works in shell but not in script [duplicate]

I frequently have to cd from $HOME to a particular long directory path. So I thought I'd put a cdscript in $HOME to make getting there a little quicker. cdscript: #!/bin/sh directory="/some/big/long ...
1
vote
0answers
25 views

Output Tmux Output to the Terminal

I'm using supervisor to monitor keep a process alive, but I do need to have contact with that process every now and then to provide user input. I would normally use tmux for this, but when I use ...
5
votes
4answers
367 views

Remove next-to-last extension in a file name

Suppose a folder with many files with bad extensions,for example Song1.avi.mp3 Song2.avi.mp3 Song32.web.mp3 Song23.mp4.mp3 Song2a.mp9.mp3 I want to remove only the second field (web, avi, mp4). I ...
-1
votes
2answers
27 views

How to compare strings in Bash script [closed]

I have one strings like "9999991385" now I want to match first 4 digits from one file if it's match or not ??
13
votes
4answers
804 views

How do I run a command and get its exit code in fewer characters?

I used to write my scripts like this: some_command while [ $? -ne 0 ] ; do sleep 1 some_command done It always irked me that I have to write some_command twice. Can I put it inside the loop ...
4
votes
1answer
205 views

bash permission denied but sh works

A little script of mine on a mounted windows partition refuses to be executed directly (shebang is #!/bin/bash), but interestingly calling it with bash/sh works. The script is simply a collection of ...
4
votes
1answer
116 views

Script for “battery level reminder”

I want to plug in the laptop battery charger when the battery level goes down to 40% and then plug it out when the battery charge level reaches 80%. And for that reason, I need a script that would ...
2
votes
1answer
108 views

What does /lib/lsb/init-functions do?

Can anyone help me in understanding the meaning of the following code from /lib/lsb/init-functions: base=${1##*/} Also it would be helpful if it was possible to explain how pidofproc function ...
2
votes
3answers
50 views

Is there some built-in (bash/gentoo) function to provide well-readable output to the user?

I am using the following simple function to provide some well readable output to the user. Now I am wondering if there is some kind of (gentoo-/bash-) built-in function to accomplish this task, as ...
3
votes
3answers
90 views

Redirecting stdout to terminal and file without using a pipe?

I have code, that goes something like this: #!/bin/bash VAR=0 func() { VAR=$((VAR+1)) echo 'Logging information.' } func 2>&1 | tee 'log.txt' echo "Should be 1: ${VAR}" When ...
17
votes
3answers
478 views

Finding the correct tmp dir on multiple platforms

I have a script that needs to create temporary files for its work, and clean up after itself. My question is about finding the right base directory for the temporary files. The script needs to work ...
0
votes
1answer
58 views

Debugging with shell programming and cpp files?

I am having trouble understanding how my code is working. I am new to this project and never learned unix before. My coworker quickly showed me how to run the program but didn't explain how it worked ...
1
vote
0answers
19 views

Take the unknown contents of variable as literal in sed [duplicate]

I have two files: a.patterns contains patterns in a one per line way "`" "^" "<" "<(" "<)" "<[" "=" ">" "_;|^+*+?@" and b.data contains comma separated values. 1,1,0,"*",1 ...