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

learn more… | top users | synonyms (2)

2
votes
3answers
127 views

Find files newer than a day and copy

I am working on a script that will copy ONLY files that have been created within the last day off to another folder. The issue I am having is the script I have copies all of the files in the source ...
6
votes
1answer
174 views

Is it better to use $(pwd) or $PWD?

I encountered BASEDIR=$(pwd) in a script. Are there any advantages or disadvantages over using BASEDIR="$PWD", other than maybe, that $PWD could be overwritten?
1
vote
3answers
53 views

Calculate and round numbers up in bash

I'm struggling with a slight variation on: How to calculate by taking first field and How to round floating point numbers in shell? I have a file that looks like (space between columns): 1533 ...
4
votes
1answer
99 views

cmd2 `cmd1` vs cmd1 | xargs cmd2

I'm trying to find a file with find and then pass the found path to sqlite3. I'm only dropped into the sqlite> prompt if my command is of the form: sqlite3 `find . -type f -iname "*.db" | head ...
3
votes
1answer
137 views

bash: non blocking read inside a loop

I have a following small bash script var=a while true : do echo $var sleep 0.5 read -n 1 -s var done It just prints the character entered by the user and waits for the next input. ...
1
vote
1answer
51 views

Is there a simple way to find out the last line executed in a bash script?

In answering this question please bear in mind that I am a beginner and a bit out of my depth. I'm running a bash script which contains a for loop which will run thousands of times. Somewhere in this ...
2
votes
2answers
95 views

How to retroactively make a script run as root?

I'm writing a shell script, that needs to be run with root privileges. I can check if a user has root privileges with sudo -nv || echo "no sudo", but that doesn't help me, if his credentials are ...
0
votes
2answers
52 views

Command to see if a word is in a list of files and output how many files it is in

There is a similar question to mine already but mine has a slight difference. I want to see how many files contain each word. When a file has a word then it increments the counter and then continues ...
0
votes
1answer
32 views

Syntax of process substitution

I am really having some trouble understanding pipeline substitution. I have a program that I run with the make command that writes to stdout. What I want to be able to do is parse through the output ...
4
votes
2answers
43 views

Is there a way to have a function in my bash script auto run on any command error?

I'm writing a shell script that needs to do a bunch of commands and every command depends on every previous command. If any command fails the entire script should fail and I call an exit function. I ...
1
vote
1answer
86 views

Storing longer text output in memory in shell variables vs Writing to disk

I have the below call to db2 command line tool inside a loop which runs 100k iterations. (the db2 output is 5 rows of 20 chars approx per each call, total of approx 100k calls. The input to SP is ...
23
votes
2answers
2k views

Why does ( echo “”; 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 ...
0
votes
2answers
45 views

Using variable with awk -v in a shell script

I am modifying/re-writing some of the scripts written by former employees of my company and in one of the shell scripts I found the following line. b=`benchmark=30;grep "Waiting for timer" wk.txt | ...
1
vote
1answer
37 views

How to continue a script after X has been closed?

I recently installed RetroPie on my Raspberry Pi and I'm trying to find a way to (once I'm in LXDE) run a shell script that kills X and then starts EmulationStation. I can kill X with pkill x but then ...
0
votes
2answers
31 views

output a variable value using echo command [duplicate]

I want to output number of lines in text file.So I am using echo command but I am getting filename along with number of lines when I run the following code: echo "$page" > t total="$(wc -l t)" ...
2
votes
2answers
58 views

Check files for conventions

I need a script that checks and lists all text files for project conventions. With conventions I mean for example: UTF-8 encoding No trailing white spaces Newline at the end of file No not-ascii ...
0
votes
1answer
23 views

Count number of times each line repeats itself, In case Insensitive manner

I have a file like a A b c I want to print number of times each line repeats it self in a case insensitive manner. I found below command to be useful: tr A-Z a-z | uniq -c | sed -e 's/^ *//' -e ...
2
votes
3answers
153 views

find, xargs and mv: renaming files with double-quotes, expansion and bash precedence issue

Ok, I'm simply trying to strip out double-quotes in my filenames. Here's the command I came up with (bash). $ find . -iname "*\"*" -print0 | xargs -0 -I {} mv {} {} | tr -d \" The problem is the ...
-2
votes
1answer
49 views

Sorting a file based on second column in a Tab Spaced file [closed]

This may be a duplicate, But I have a Tab separated file and I need to sort the file based on second column which are float values. sort -s -k2,2gr -t$'\t' The code states it is to sort based on ...
1
vote
3answers
48 views

Evaluation an expression and rounding up to three decimals

I have an expression "5+50*3/20 + (19*2)/7" I need to round it up to 3 decimal places. The answer to this is 17.92857142857143. When I use the script below it is giving me 17.928. The answer should be ...
1
vote
6answers
384 views

Print numbers from 1-50

x=1 while [ $x -le 50 ] do echo $x $x=(($x + 1)) done I have wrote the above code. What seems to be a easy task in many programming languages is giving this error for me. solution.sh: line ...
3
votes
2answers
45 views

Script to replace all symbolic links to a target to point to another target (in the same directory)

Given the following contrived example: $ mkdir -p a/b && touch a/b/original-target && touch a/b/new-target $ mkdir -p c1/c2 && cd c1/c2 && ln -s ...
2
votes
1answer
60 views

Bash variable ${0##*/}

I am trying to understand the variable ${0##*/} that I encountered in a bash script. I understand that $0 contains the name, or the path, of the script, then ## works as in ${parameter##pattern} ...
2
votes
3answers
121 views

Command for finding users who are not logged in

Is there any command that can find the number of users that are not logged into the system ? I am trying to write a shell script that counts the number of users who are logged in and number of users ...
2
votes
5answers
214 views

cd into all directories, execute command on files in that directory, and return to previous current directory

I'm attempting to write a script that will be run in a given directory with many single level sub directories. The script will cd into each of the sub directories, execute a command on the files in ...
4
votes
1answer
148 views

string in a file as a variable

I have a conf file content like below. file.conf: key="name" value="pswd" Now I want write a script to having two variable like above script.sh key="name" #key as variable value="pswd" ...
0
votes
3answers
107 views

Shell script to add or replace block of text?

I'm looking for a shell script (Bash) that adds or replaces copyright text at the beginning of the file (for .c and .h files). It should search and add or replace the copyright text block for all the ...
0
votes
0answers
18 views

Get CPU time for Benchmark in Bash Script

I'm using this Method here: http://giantdorks.org/alain/how-to-benchmark-individual-shell-script-sections-to-identify-slow-parts/ to Benchmark my Bashscript: RuntimePrint() { duration=$(echo ...
1
vote
2answers
37 views

how do I copy files like Microsofts “Robocopy” do in debian?

I've been trying to migrate small parts from my media/download center from windows to linux, before making the jump on my media center. Right now i have a Raspberry pi set up, with raspbian, where i ...
1
vote
0answers
15 views

Pass/Firefox master key integration?

I handle my passwords with pass, and I use the Firefox master password functionality. The master password is rather long and "safe" and stored in pass along with the others. For now, my usual Firefox ...
1
vote
0answers
24 views

remote control of vlc using script over ssh

I'm trying to control vlc video playback on a remote machine using a script and remote ssh. I have machine A, which logs into machine B with this command: # sshpass -p <password> ssh ...
3
votes
1answer
46 views

Use paste with row-major input

I can create a file with multiple columns from a single-column input via paste like this: some_command | paste - - This works when some_command produces the data in column-major format. In other ...
1
vote
2answers
36 views

Paste text to another application from script via shortcut

Let me describe what I want to achieve. We are using two-factor authentication and I would like to create script which will generate TOTP token for me and it will paste it to let's say Firefox on some ...
6
votes
4answers
375 views

Detect if a variable is empty

In my Bash script I have this line: mv -f $HOME_DIR/$dir $HOME_SAVE/BACKUP The problem is when $dir is empty (no $dir directory) the mv command moved the directory $HOME_DIR. We want only to move ...
0
votes
0answers
24 views

Insert values from one database table to other database table through shell scripting in Ubuntu 13.10

I am working with 2 databases say logdb.db and logdb2.db . I have created a table named Original in logdb.db Sqlite database such as follows : CREATE TABLE Original (s_id INT PRIMARY KEY NOT NULL ...
0
votes
5answers
47 views

Is there a way to do a “full file buffering” inside a chain of piped commands?

Very often I do simple operations on a file, for example: cat file1.txt|sed -r 's/^ *//' to remove spaces at the beginning of the line. If I want to overwrite the file, the only way I know is: cat ...
37
votes
1answer
806 views

Security implications of forgetting to quote a variable in bash/POSIX shells

If you've been following unix.stackexchange.com for a while, you should hopefully know by now that leaving a variable unquoted in list context (as in echo $var) in Bourne/POSIX shells (zsh being the ...
1
vote
3answers
41 views

Splitting a string up to a specific location

I have a question and wondering how to address it with sed? How does one cut a variable up to a specific location, in my case _g? Example variable="This_is_good_g0r0s0_continues" I need to cut ...
2
votes
1answer
73 views

Reading two lists containing filenames

I am taking two lists having filenames with paths and using gnu-parallel to process the files from the two lists. However,The command is able to use only files from the first list and not the second ...
0
votes
1answer
20 views

executing commands in shell and turnoff the default displays of commands in console

This is my script cd /opt/product/vinoth -- some operations -- cd - While executing this command cd - the old path get displayed in the console. is there is any way to block this from displaying ...
-1
votes
0answers
25 views

Ping Printer then Execute Command

I have many printers on my network using CUPS and a few pause occasionally. I can't find any consistency between them to isolate the cause so in the meantime I've made a script to view all paused ...
4
votes
2answers
39 views

How do I convert a string into a number interpreted in certain base in bash script?

I'm simply trying to convert a string $RECORD_HDR to a number $RECORD_SIZE, where echo $RECORD_HDR gives 003D and echo $RECORD_SIZE should give 61
0
votes
1answer
69 views

Creating a list containing filenames with paths

I have a directory(INPUTDIR) with sample names as subdirectories(508_C,540_C,570_D etc).Within those each subdirectories there is another directory called FASTQ which contains two kinds of files. ...
0
votes
4answers
56 views

Print last character in the given path

I am trying to concat "comnp.dat_CY*" files into one file. File name, source path and destination path are the parameters. I am trying to find the last character in the path to check if it is "/" or ...
0
votes
0answers
14 views

Simple slide show tool for raw and jpg with playlist

I'm looking for a tool / bash script that can show raws and jpgs from a playlist file in fullscreen. I plan to obtain a list of image file paths and I want to be able to manually navigate from image ...
9
votes
5answers
698 views

Get total duration of video files in a directory

I have a list of .ts files: out1.ts ... out749.ts out8159.ts out8818.ts How can I get the total duration (running time) of all these files?
2
votes
2answers
44 views

Command to work on all files in a folder and output result to single file

I have a list of files, 10 to be exact, in a recipe folder. I must determine the number of occurences of each word in all the files and then order them according to these frequencies. I can get my ...
5
votes
1answer
133 views

How to make a list generated by md5deep in alphabetical order of relative paths?

If I want make a md5sum list recursively, then I would use md5deep, but it starts to pop up some problems such as it won't generate the md5sum file in alphabetical order. For example, $ cd ...
-1
votes
0answers
19 views

how to pass command line arguments from c programm to the bash script? [migrated]

i have written one bash script and now am calling this script from c program. Now i want to pass arguments i.e. argv[1] and argv[2] to the script from command line.
2
votes
2answers
34 views

Checking and Killing processes of a user before deleting

I am working on a script to delete users from text file. However, before deleting, I wanna check if any process by a user is running and if running then kill all the processes and delete. Here's how, ...