Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [bash]

Questions specific to GNU’s Bourne Again SHell, as opposed to other Bourne/POSIX shells. For questions about Unix shells in general, use the /shell tag instead.

3
votes
3answers
184 views

Resolve Variable in a Variable in shell

I have to use environment vars in my bash shell and: echo $ENV resolves to DEV, STAGE or PRD depending upon the stack. echo $DEV_ACCOUNT resolves to a number e.g. echo $DEV_ACCOUNT gives 12345678 I ...
0
votes
2answers
39 views

Bash script: I am checking files by ls -1, I also want to loop if there are no files

I need to check if there are 12 files landed in a dir. This loop works fine when there is a one file and it will loop 4 times with "sleep 300". but when there are no files at all, it fails and does ...
2
votes
1answer
39 views

Keep sourced file after script execution [duplicate]

Is it possible to keep source from an executed script without sourcing it ? Here is the script install.sh: JJJ=bar When i execute the script like ./install.sh, after execution, i got the following ...
1
vote
2answers
30 views

how to awk user input [duplicate]

I'm trying to match a pattern and then print out two lines after it. I'm successfully able to do it with the following code. awk '/'202404'/{c=3}c&&c--' consolelog.log > testing.txt I'm ...
1
vote
0answers
40 views

Get/Set environment variables in a different tty

I'm using a tool (Autokey) that spawns in a new shell. Cool tool, but I want to pull info from my current shell. Say I'm on pts/0 and I have a variable set: VAR1=yes. In another shell, I want to be ...
8
votes
3answers
631 views

Flattening directory hierarchy preserving directory names in new directory name

I basically want to go from this: . ├── Alan Walker │   ├── Different World │   │ ├── 01 Intro.mp3 │   │ ├── 02 Lost Control.mp3 │   │ └── cover.jpg │   └── Same World │   ├── 01 Intro.mp3 │...
2
votes
1answer
44 views

Look for multiple line string from bash

I am currently working with Jupyter notebooks. With an IDE I used to be able to look for parts of my code that could be elsewhere, and thus refactor as needed. Suppose I have the following code: ...
10
votes
4answers
1k views

Move all files NOT ending with .txt [duplicate]

In the directory /home/username/data I have both files and directories. Some of these filenames end in .txt (to which I'll refer as text files), others don't. The same happens in the subdirectories. ...
1
vote
1answer
37 views

Prevent quote expansion in user-made function [duplicate]

I have the following bash function : tg() { git add -A && git commit -m $1 && git push } But it seems not to work : $ tg "create index for users" error: pathspec 'index' did not ...
4
votes
2answers
324 views

chown: missing operand after ‘root:users’

I try to change owner to root:users recursively below a directory, if owner is other than root:users. cd /dir/ find . \( ! -user root -o ! -group users \) -print0 | xargs -0 chown -vc root:users I ...
-1
votes
2answers
91 views

Output colored text in all major distros without using non-word color codes (like \033[0;31m)

I found these ways, for example, to output colored text in a simple way to the screen: RED="\033[0;31m" # Red color (via ANSI escape code); NC='\033[0m' # No color (via ANSI escape code); echo -e "${...
0
votes
2answers
37 views

Syntx error near unexpected token '}'

i am trying to write a function but I am getting Syntax error. Below is my function: checkNoOfParameter () { if [[ ${1} -eq ${2} ]] then job_Status = $true else job_Status ...
1
vote
1answer
52 views

How can I make `read` read from stdin? [duplicate]

bash builtin command read is said to accept input from stdin, but why does the following not read in anything? $ printf "%s" "a b" | read line $ printf "%s" "$line" $ Thanks.
0
votes
2answers
161 views

Why is this string matching condition in [[ ]] not true?

$ tpgid=$(ps --no-headers -o tpgid -p 1) $ echo $tpgid -1 $ if [[ $tpgid == "-1" ]]; then > echo "yes" > else > echo "no" > fi no Why is the condition not true? Thanks. $ printf ...
2
votes
2answers
243 views

Bash: Timer in while loop

I have a while loop in my script which waits for the connection get online and then continues. #!/bin/sh while ! ping -c1 $1 &>/dev/null do echo "Ping Fail - `date`" done echo "Host ...
4
votes
1answer
235 views

Is it possible to check if a specific process is sleeping or running?

I've created the following script on Ubuntu that can pause and start a specific process: #!/bin/bash loopProcess () { COUNTER=0 while [ true ]; do echo $COUNTER sleep 1 let ...
1
vote
1answer
101 views

Displaying files and increment them +1

I'm creating a script that lists all the nano files in a directory. han than the files are added with a number. (Adding +1 for every file). Then the user is allowed to view the nano file. Here is ...
1
vote
1answer
29 views

GPIO board read/write via bash

Trying to get a GPIO board working, need to access it from script, here is what I am using: stty -F /dev/ttyACM1 115200 raw -echo #CONFIGURE SERIAL PORT exec 3</dev/ttyACM1 #...
1
vote
1answer
30 views

Locating font files

There's a utility called fc-list in the fontconfig library, that's good for listing installed fonts. It detected a few hundred system fonts. I ended up using find to locate all files with common ...
1
vote
1answer
41 views

Reading and writing multiple files with same file extension

I have three different file sets say 10234.su, 10234.ph and 10234.th. I have a shell script using which I can extract some data from 10234.su and can write to 10234.ph and 10234.th, but it works for ...
1
vote
3answers
85 views

Why does closing terminal emulator window terminate a bash process with SIGHUP trap changed not to terminate?

In a bash shell in a terminal emulator window of lxterminal, I run $ trap "echo hello" SIGHUP $ kill -s HUP $$ hello $ and then I close the terminal emulator window. Does closing the terminal ...
0
votes
1answer
37 views

same script on servers get error

I have this script on about 20 servers. CASE=$1 case $CASE in "multipath") status=$(service multipathd status | awk '{gsub(/[.]/,"");print $NF}') if [ $status = 'running' ]; then ...
1
vote
0answers
40 views

Define function without name and immediately call it [duplicate]

I have a script file (it's .bashrc actually), in this file I have a lot of variables and functions which are used only inside this script, but these variables and functions are publicly visible for ...
1
vote
3answers
106 views

Is there a character as `delim` of `read`, so that `read` reads the entire of a file at once?

In bash manual, about read builtin command -d delim The first character of delim is used to terminate the input line, rather than newline. Is it possible to specify a character as delim of read, ...
1
vote
1answer
51 views

Bash script to open tmux but can't see username

I have a bash script that starts a tmux session with some monitoring tools, and have a cronjob that runs it on startup; however, I can't see the usual prefix to my commands, and I can't use any arrow ...
9
votes
2answers
361 views

How to create alias with a command contains ' and "

A few posts ago someone asked how to show memory in percentage. Someone replied with: free | awk '/^Mem/ { printf("free: %.2f %\n", $4/$2 * 100.0) }' I was wondering if I can turn this command into ...
0
votes
0answers
35 views

byobu prevents SSH login

I'm running arch linux for arm on an old goflex for network storage and wanted to use byobu on it. There's no package for the distribution so I compiled it from source, and it works just fine. The ...
0
votes
2answers
43 views

A bash script to square a tall image of a long text column?

Ever seen a print of a long discussion thread? Looks like the picture of a skyscraper… with only the building in it! So what I generally do — manually! — is break up the image into segments, and then ...
1
vote
0answers
53 views

How to script AT command to modem?

Every time my USB modem (/dev/dlinkModem) gets unplugged it defaults to "radio disabled mode" (AT+CFUN:4). I can easily enough call AT+CFUN:1 through the minicom application console to enable radio ...
1
vote
2answers
92 views

Telnet connection stops bash script

I have a bash script where I continuously read from a text file the hosts and port and create a connection to check for the status. My code is: while IFS=";" read -r host ports do eval "arr=($...
1
vote
4answers
82 views

Count the number of files and folders containing a certain string

Let's say I'm in the Desktop directory and want to count the number of files and directories whose name is exam. It should also count the files containing the string exam. So to sum it up I need to: ...
2
votes
6answers
252 views

Exit shell script containing indefinitely running java program

I have an autosys job. In the autosys job I am executing a shell script. Shell script executes indefinitely running java program. Say infinite for loop. #!/bin/ksh function abc() { java ...
3
votes
1answer
45 views

What's the purpose of 1> in exec 1> >(logger -s -t tagname) 2>&1?

I just stumbled over exec 1> >(logger -s -t $(basename $0)) 2>&1 which is used to redirect the output of the current script to the system logger (in case you've never seen this, but are ...
2
votes
1answer
52 views

What is it “!!” and how it connected to `$_` or `$?`?

I was playing with string substitution while learning a bit more of bash, and I have add !! into some dummy example echo ${TEST/hey/!!}... I was surprised that !! was replaced with last entered ...
1
vote
1answer
70 views

Grep a string in a file in bash [duplicate]

When I grep a string in a file, it gives me a match. However, when I do it in a bash script, it doesn't work. $ cat files_android.txt 000d07dfe5016314c98b869c19c7f986b5db57db49ac76d16a5d2f5861a35072 ...
0
votes
0answers
16 views

Possible to override bash environment variable completion?

I'm trying to figure out how to use the complete bash builtin to override tab completion of "environment variables." That is, when there is a dollar sign $ to the left of the cursor, with all ...
0
votes
1answer
24 views

Pass space separated arguments stored in variable to command

I've hijacked a pretty neat backup script from the internet but somewhere along the lines there is something like this going on DIRS="/home/ /var/www/html /etc" tar -czf /backup/file.tar.gz "${DIRS}"...
0
votes
1answer
49 views

Why do some programs' processes exist after forking a child, without the child starting a new process session?

Here are some of the steps for creating a daemon process from a given process, the given process exists soon after forking a child, the child calls start a new session and also a new group, by ...
2
votes
1answer
54 views

Does ` (sleep 123 &)` remove the process group from bash's job control?

Does the following way $ (sleep 123 &) $ jobs $ remove the process group of sleep 123 from bash's job control? What is the difference between the above way and disown then? Note that the sleep ...
0
votes
2answers
54 views

Shorten very long dir name in bash prompt

How to get from this prompt ~/this/is/a-very-very-long-directory-name/dir to this ~/this/is/a-ver...name/dir in a bash prompt? So shorten directory names longer than nn (20+) characters to ...
0
votes
2answers
31 views

How do I determine whether components of an array where numbers and letters are combined succeed each other on the basis of the numbers?

I have an array with contents: ${array[0]}="h4", ${array[1]}="q2", ${array[2]}="s7", ${array[3]}="r1", ${array[4]}="h3" and i want to determine if the numbers in those arrays (4, 2, 7, 1, 3) succeed ...
0
votes
2answers
54 views

Redirect output to standard out instead of file

I am writing a java wrapper for convert (which is part of image magick). What I want to do is call a command like convert -thumbnail 1024 test.JPG >(cat) to make it print the thumbnails byte stream ...
3
votes
2answers
103 views

Send a notification or alert when bash asks for input from user

I am using Ubuntu 16.04 LTS. There is an alias in my .bashrc which uses notify-send: alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -...
0
votes
2answers
41 views

Counting the number of lines between same repeating pattern

I have refer this Grep the lines between the occurrence of the same pattern , I don't want to split into file; instead, I want to store it into an array. File count.txt contain: 0 1 2 3 0 1 2 0 1 ...
3
votes
1answer
187 views

how to set parameters when pipe bash script to bash

How to execute bash script with parameters: ./foo.sh a b c When it's compressed (e.g. using xz). xzcat foo.sh | bash <<how_to_supply_here_parameters?>> Specific usecase: I produced ...
-1
votes
2answers
60 views

How to make Readline in vi mode to read .vimrc?

When using Readline's vi mode in Bash, I can use command v from normal mode, to open the current command line in Vim for editing. However, in this case, Vim does not read my ~/.vimrc file, so none of ...
4
votes
3answers
67 views

Replacing all instances of string containing hyphen

Running the following command: echo "hello-world" | tr "hello-world" "Hello World" Returns Heddd-ddddd. I want it to return Hello World. I believe this is because tr is interpreting the SET1 ...
2
votes
3answers
134 views

How do I sort an array with letters and numbers combined in bash? [duplicate]

I have an array with h4 h5 h1 h2 h3 in it and I would like to sort it according to the numbers, but don't know how. What is the best way to do this? edit1: I would also like to sort an array via the ...
1
vote
1answer
44 views

Printf - print repeating minus symbols [duplicate]

how can I use printf to print a row of minus symbols? when I try: printf "-----------\\n" I get: bash: printf: - : invalid option printf: usage: printf [-v var] format [arguments] when I try: ...
-1
votes
0answers
42 views

bash shell script for following situation?

I would like to use a bash script for some .raw image merging. I have 4 .raw images in directory /home/output and the file names are 1.raw, 2.raw, 3.raw, and 4.raw. All the files (.raw) have the same ...