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

learn more… | top users | synonyms (2)

0
votes
3answers
82 views

Need some help on grep command on shell script

I have a log file and I am trying to write a shell script to find multiple strings in a file on multiple lines. For example if I see the phrases ...
3
votes
2answers
73 views

Why is #!/usr/bin/env bash not working on my system?

I ran into some issues when running some installation scripts where they complained of bad interpreter. So I made a trivial example but I can't figure out what the problem is, see below. ...
1
vote
3answers
31 views

Print array line by line in a here doc

I currently have the following here doc message=`cat <<-EOF Test data: Test data 1: "${Test_data_array[@]}" EOF` echo "$message" Is there a way to get the array to display multi-line per ...
0
votes
1answer
49 views

How can I add all my groups to a different user in bash?

I've got a user named foo. I'd like foo to have all the same group memberships that I do. I thought I was close, with this: sudo usermod -a -G $(id -Gn | tr " " ",") node4 The id piped to tr is ...
0
votes
0answers
32 views

Git “unknown revision” error when within bash script

I'm writing a shell script that needs to perform certain operations based on the last commit's message of my local branches with a name starting with "task". Here's what my script looks like ...
4
votes
4answers
263 views

files greater than a average size of directory?

how could I find all the files and directories with size larger than or equal to the average file size of the current directory?
3
votes
2answers
64 views

How does the -d option to bash read work?

I have a bash script where I'm trying to assign a heredoc string to a variable using read, and it only works if I use read with the -d '' option, i.e. read -d '' <variable> script block ...
1
vote
1answer
43 views

SIGINT handling differences between bash 3 and 4

I have a script which works great in bash 4.3, but gives me unexpected behavior with bash 3.2. Here's a simplified version: set -o errexit -o pipefail task() { local name=${1} local ...
0
votes
2answers
42 views

How to merge files with same names

I want to know how to merge files with same names For example I have following files in one folder: 1081713FHN-NA.one_.txt 1081713FHN-NA.second_.txt 1081713UEN-NA.one_.txt 1081713UEN-NA.second_.txt ...
1
vote
2answers
69 views

BASH how to save output of rm with timestamp?

I nearly have this working, This is a script that will run in CRON to keep the latest 10 backups, and remove anything else, I want to log this to keep track to make sure all is in good order, I have ...
0
votes
1answer
55 views

Symlink to file which does not exist yet

I'm currently trying to cleanup my home directory by moving files like .vimrc, .bash_profile, etc. to a directory .dotfiles in my home directory. The idea is to use symbolic links to these files ...
0
votes
0answers
78 views

How to redirect linux command outputs to specific row and column in csv?

I'm working on Unix Build post documentation process, I have a CSV file to be filled in and uploaded for every new build, to ensure the servers are build as per the original request. The CSV file ...
1
vote
1answer
20 views

writing output of xbacklight -get to a variable in a bash script

I have a little script that lets me set the display brightness #!/bin/sh VALUE=$(zenity --scale --text="Select Display brightness" --value=50 --title "Helligkeit anpassen"); xbacklight -set $VALUE; ...
0
votes
2answers
37 views

Input redirection using command and options

I am trying to write a script that takes in 2 parameters - program name and optional flags. I am trying to execute the program command on an input_file and store the output in the output_file. ...
0
votes
2answers
75 views

Grep / awk on multiple files to single output

I have several txt-files containing data, where i use grep to search for a current string of text, and use awk to filter out the variable i need. The string is repeated through the file, so i ...
3
votes
1answer
28 views

Head Script output explanation

So this shell script: #!/bin/bash head >/dev/null; head; almost always gives the same output when called with sequential numbers (e.g. seq 10000 | ./sscript) OUPUT: //blank line 1861 1862 ...
3
votes
2answers
39 views

How can I get rid of all script blocks in html files? [duplicate]

How can I remove all script blocks (including multi-line ones) from html files such as: <script type="text/javascript"> var googletag = googletag || {}; googletag.cmd = googletag.cmd || []; ...
2
votes
1answer
23 views

Repeat based off user input OS X

I am creating a script which asks a user to enter a message then how many times they would like to display that message. This is what I have so far. message.sh #!/bin/bash echo -en "Enter a ...
2
votes
3answers
65 views

log out via shell script

I am doing some operation via shell script. If that script is successful, continue else logout of the current user. I am unable to find a way to logout of current user. I tried to use exit but seems ...
3
votes
2answers
44 views

confirmation before sudo su

most of the person in my team have sudo access. But I still want them to confirm before they do sudo su. if user press Y, then allow him as root else return him to his own home directory. I tried the ...
2
votes
2answers
37 views

Find commands/function whose name matches a value

I am looking for some command e.g. match ls which should match commands like ls, alsa asls,.. and return them. I preferably want it to cover both all commands and defined functions. Is there a builtin ...
0
votes
0answers
51 views

Shell scripts for proof reading

I am using below mentioned script present at link to check my writing. But, while running the scripts some syntax errors are shown up. I am not able to correct the same. Haroons-MacBook:democheck ...
0
votes
1answer
30 views

How to stop an infinite loop function called with myFunc &

In my bash script, I have a function that does one certain task in an infinite loop. To allow for the next part of my code to run at the same time, I run it with myFunc &. How can I kill this ...
0
votes
1answer
19 views

Bash script hangs on specific input

I am trying to write a simple script which does something if the first parameter of the input is equal to the -r string. But whenever I try to run this file, the program hangs and I have to terminate ...
9
votes
9answers
315 views

How to get the last argument to a /bin/sh function

What's a better way to implement print_last_arg? #!/bin/sh print_last_arg () { eval "echo \${$#}" # this hurts } print_last_arg foo bar baz # baz (If this were, say, #!/usr/bin/zsh instead ...
1
vote
1answer
53 views

get first CLI argument after the options in shell scipt

I have a script I class like so gitploy up -t 2.0.0 test_repo. I pull out the "action" up right away, then I need to be able to get the test_repo before I process the options. I don't want to lose ...
1
vote
1answer
24 views

Breaking out of a loop, return to start of loop with correct value of variable

I am sending processes to a server, but I only want a certain number running at a time. Thus I loop which checks how many jobs are running and only submits new one if the current amount of jobs do not ...
0
votes
1answer
21 views

Trying to find the index of an entry in a list [duplicate]

I am trying to find the index of an list entry using this simple method: #!/bin/sh partList="500 1000 2000 4000" a=( $partList ) echo ${a["500"]} this returns Syntax error: "(" unexpected. And the ...
-1
votes
2answers
26 views

Script not running! Also how to attach timestamp in mail?

I have the following bash script... #!/bin/bash # Delete JUNK temp folder if [ -d JUNK/FOLDER/PATH ] then rm -rf JUNK/FOLDER/PATH echo "Cleansed JUNK directory successfully!" /usr/bin/mail -s ...
0
votes
1answer
43 views

set date time from user input in shell

I am creating script to check RTC working & for that first I am getting the date & time from user input in yyyy mm dd hh:mm:ss format. read -p "Enter the date:" val echo $val date -s "${val}" ...
1
vote
1answer
44 views

Why do I get the message set no match when I run this script?

#! /usr/bin/tcsh -f set ps_output = "`ps -u $user`" @ i = 2 echo "$ps_output" set ps_test while ( $i <= $#ps_output ) set line = ( $ps_output[$i] ) if ( $line[4] != "ps" && ...
0
votes
2answers
31 views

How to assign a multiple line string value to a variable with indentation in a shell script?

I want to assign the following multiple line string value to a variable in a shell script with the the exact indentations and lines. Usage: ServiceAccountName LogFile Where: ServiceAccountName ...
0
votes
1answer
31 views

What are conditions I can check for detecting error in downloading using bash?

I want to continuously download a link until it successfully downloads, #!/bin/bash while [ some_condition ]; do curl my_url # or vagrant add my_vagrant_url ...
0
votes
0answers
13 views

How to generate total size of files from each unique source in bash

I have a large number of directories which each contain files with the format unimportant_data_here_source_date.gz I want to calculate how much data came from each source. That means figuring out ...
1
vote
1answer
27 views

How to programatically send break signal (Magic SysRq) through serial terminal

I have following setup - I have 2 computers connected by serial connection (null modem) emulating vt220. I'd like to send break signal (Magic SysRq) from shell script - is it possible without writing ...
1
vote
0answers
42 views

Bold and Underline using echo with email

I am using a script that emails me a particular status report. The script sends me the contents of several files. I am using echo to provide a heading in the email that is used to differentiate ...
2
votes
1answer
74 views

How to get filenames when using find and sed

I am writing a script to apply sed on certain files and then list files that have been changed so that I know which have been modified. This is how I am finding and then using sed: find . -type f -a ...
0
votes
0answers
36 views

How do I redirect the output which is generated in that new terminal

I am trying to redirect the output from a newly invoked terminal to a file. But I don't know what the problem is with this code. Nothing is displayed in the new terminal. #!/bin/bash xterm -hold -e ...
0
votes
1answer
30 views

Can someone please explain this error I'm getting in my script

I'm trying to create a script which I can set as a cron job which adds all my dotfiles to a folder I've set up as a github repo then push them every evening. My script is the following. cp ...
0
votes
1answer
49 views

Using sed on files with certain extension inside shell script

I am trying to use sed to find files with certain extension and then replace occurrences of a certain string with another. No directory mentioned using current directory *.h sed: can't read *.h: No ...
1
vote
0answers
51 views

File exists but errors with “mv: cannot stat: No such file or directory”

For some reason my script cannot copy or move files that exist. It comes up with the errors below. Can someone tell me why? cp: cannot stat '/mnt/sd/bbu/install/scratch/bin/.': No such file or ...
2
votes
4answers
52 views

Change dir from script without a “shell procedure” or “sourcing the script”

I know what I can make a shell procedure or source the script instead of running it to cd the shell I am using. What I want to know is there any way to cd the "parent shell" (not sure if that is the ...
1
vote
0answers
21 views

Piping to 'tee' commands prevents changes in environment variables

I have a csh script using which I modify certain environment variables and echo values of these environment variables from within this script. I source this from another script: source ...
6
votes
3answers
1k views

Executing mail command from inside a function causes a “fork bomb”

When I try to execute mail from inside a function in a bash script it creates something similar to a fork bomb. To clarify, this creates the issue: #!/bin/bash mail() { echo "Free of oxens" | ...
0
votes
1answer
37 views

How can I get many addresses from many trajectories with bash script without error?

I have many trajectories data. 500k. I try to get adresses of these coordinates. I use bash command in linux. #!/bin/bash filename="$1" while read -r line do uid=$(echo $line | cut -f1 -d,) ...
0
votes
1answer
22 views

cannot reload .profile from script

I am trying to reload .profile from the below script, but that doesn't work at all. #!/bin/ksh sed -i '/export rel=Rel1510_Linux/c\export rel=Rel1602_MX' ~/.profile source ~/.profile When I run ...
0
votes
1answer
40 views

bash script : How to backup /user/home?

I'm currently studying scripting and I need to create a script to backup the /user/home using .bz2. I need the script to verify if the user exists and if there was no user selected to backup. ...
3
votes
7answers
326 views

Check if shell variable contains an absolute path

I want to check if a shell variable contains an absolute path. I don't care if the path exists or not—if it doesn't I'm going to create it—but I do want to ensure that I'm dealing with an absolute ...
0
votes
1answer
29 views

How do I remove a . from the start of a Bash string and write the result to a variable? [duplicate]

I am presently writing a shell function and it would help me in writing it to be able to remove a . from the start of a folder name (changing the folder name from that of a hidden folder to that of a ...
1
vote
2answers
33 views

How do I keep command options from breaking other scripts executed within my command?

I have a bash shell script(my_script.sh) which runs a script(their_script) during execution. My script (my_script.sh) looks like this : THISDIR=`dirname $(readlink -f $0)` main() { cd $THISDIR ...