Linked Questions

1
vote
2answers
2k views

What are the specific naming conventions for variables in bash shell scripting language? [duplicate]

Is it the same convention as those used in known programming languages such as Java and C? I'm trying to create a lot of variables and I want it to start with a number, but I'm not sure if it's okay.
78
votes
12answers
38k views

Why write an entire bash script in functions?

At work, I write bash scripts frequently. My supervisor has suggested that the entire script be broken into functions, similar to the following example: #!/bin/bash # Configure variables ...
13
votes
2answers
1k views

bash script: fixed sleep time is adding up in while loop

I am new to bash scripting and started out with a few sample scripts. One is: #!/bin/bash SECONDS=5 i=1 while true do echo "`date`: Loop $i" i=$(( $i+1 )) sleep $SECONDS ...
7
votes
6answers
12k views

Check command success in bash

I'm currently writing a small script to backup bulks of floppy disks and format them afterward for later use. I use dd to copy the disk's image and cp to copy all the files on the disk. Here are the ...
4
votes
2answers
1k views

What is the term to describe the naming method "MyVariable" (as opposed to "myVariable")?

Variables and procedures in JavaScript are often named via the "camelCase" naming method, as: myVariable Any letter of the only or not-only-but first letter in an expression which acts as ...
1
vote
2answers
7k views

Bash can't run command from script: mkdir command not found [closed]

I have separate scripts for tasks in bash. Here is the broken one: #!/bin/bash PATH=/home/name/ mkdir $PATH cd $PATH && echo "done." exit 0 Today it broke and first time it simply didn't ...
2
votes
3answers
3k views

Iterate through multiline string line by line

I would like to process a multiline string and iterate it line by line, in a POSIX shell (/bin/sh) on a BSD platform. Bash is not included in the base BSD-distribution and has a GPL license - so I am ...
4
votes
1answer
1k views

Passing args from the bash script to the function in the script

My script: #! /bin/bash -- set -x ## docker-compose wrapper compose_fn() { local ENV="${1}" local VERB="${2}" local SERVICE="${3}" local CMD="docker-compose -f ${ENV}.yml" case "${VERB}" ...
3
votes
1answer
2k views

How do I join an array of strings where each string has spaces?

My bash script: #!bin/bash MY_ARRAY=("Some string" "Another string") function join { local IFS="$1"; shift; echo -e "$*"; } join "," ${MY_ARRAY[@]} I want the output to be: Some string,Another ...
-4
votes
3answers
12k views

How to download all files of a GitHub project with wget (in raw form)? [duplicate]

How to download all files of a GitHub project with wget? All files should be downloaded in their raw form. Already tried: wget -P ~/ https://raw.githubusercontent.com/u/p/b/* wget -P ~/ https://raw....
3
votes
2answers
3k views

How can I programmatically get this layout in tmux?

I spent quite a bit of time messing around with tmux recently, and cannnot figure out what I am doing wrong. It seems to me that some layouts are simply not allowed. There is no error or warning ...
-1
votes
2answers
5k views

How to write shell script to output two variables in one line without loop

I want to write shell script to output two variables in one line. My script is: SRC = "192.168.1.1" PORT = "22" I want to make the output looks like 192.168.1.1,22 How to do this? Thank you.
0
votes
2answers
4k views

Sleep command to run after exactly every one hour

I need your small help, i have one script which i can not schedule on crontab. But I am using sleep command. I am facing issue while using sleep command. My script took 10 or 15 min to complete ( ...
4
votes
1answer
993 views

How to use variables inside back-quotes

I have a bash script that accepts a parameter "$input" and (among other things) directs it into a program and collects the output. Currently my line is: OUTPUT=`./main.exe < $input` But I get ...
0
votes
3answers
493 views

Which quoting style GNU Bash variable definitions (mostly for paths)? [closed]

Which of the following quoting styles, for GNU Bash variables, is preferred and why? Two double quotes: VAR="/path/$V1/path with space/$V2". Multiple double quotes: VAR=/path/"$V1"/"path with space"/"...
0
votes
2answers
2k views

Eval and exec with variable substitution

I'd like to execute a statement to start a server. For that I have an environment variable to determine which server is to be started. I was given this command as a starting point: eval "exec ...
0
votes
1answer
386 views

Why is mv failing in this (bash) shell script in bash 3.2 during Mac OS recovery mode?

I'm trying to move some files using a bash script on a mac during recovery mode. I can successfully move the files by manually entering the command in Terminal after booting into recovery. However, ...
0
votes
3answers
391 views

Duplicate elements of an array except the first and last elements

I have an array, and I would like to repeat each element except the first and last elements. For example if the array has five elements 1 2 3 4 5, then after repeating, its elements should be 1 2 2 3 ...
0
votes
2answers
531 views

Extract filename and substitute

I am extracting the filename part from a full path as follows: PATH=/a/b/c/foo.o #1 BASE=${PATH##*/} #2 BASE=${BASE/%.o/-1234.o} #3 echo "$BASE" Is there a way to merge #2 and #3? Whenever I ...
0
votes
1answer
326 views

Confused about the naming convention of variables in shell scripting [closed]

I am slightly confused if there is any rigid rule regarding the naming of variables in Shell Scripting. I found different answers in different locations: Unix / Linux - Using Shell Variables (on ...
-1
votes
2answers
91 views

What does this command do (strange assignments and redirections) [closed]

I would like to get more details on what the below command is doing: PASSING=0 FAILED=0 > log_${ENV} print "Test Started at `date`\n"
1
vote
1answer
144 views

Security of netcat

I'm receiving complaints quite often that users can't hard-reset their VMs on my virtualization host. So I decided to set up some way of killing VM, without giving full access to QEMU console to users....
-2
votes
1answer
185 views

Unable to create a file with /bin/touch $FLAG encountered error is /bin/touch: missing file operand

PATH=/opt/omd/sites/icinga/var/TEST FLAG="$PATH/$1_$3.flag" | tr -d \' My $3 had single quotes which I wanted to remove, hence the above tr -d \'. Next, I ran: /bin/touch $FLAG And got: /bin/...
1
vote
1answer
159 views

Saving each line of file to different variables and use each variable one by one in command line input and output

I am new to shell scripting so apologies if this was asked before. I have a file coordinates.txt like this: 765442 866447 755343 097754 I would like to pass each value of each line of the file to a ...