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

learn more… | top users | synonyms (2)

1
vote
1answer
29 views

Crontabbed shell script not able to create/write to a file

I have written a simple shell script to backup the tables in my DB using the mysqldump command. The command simply dumps the DB into a dbName.sql file. However, if the dbName.sql file doesn't already ...
4
votes
4answers
513 views

How create a temporary file in shell script?

While running a script, I want to create a temporary file in /tmp directory. After execution of that script, that will be cleaned by that script. How to do that in shell script?
0
votes
2answers
27 views

add a new line to a delimited file

So I have a file name test.txt inside that file I have about 20 lines of text that are delimited by pipe | Example: John|freshman|seatle|math|4|fulltime Bob|senior|Tacoma|biology|4|part-time I ...
-1
votes
0answers
21 views

helping get a shell script right from github

I came across this script on github. https://github.com/loganbraga/dotfiles/blob/master/bin/runOrRaise #!/bin/bash if [ $(pgrep $1 | wc -w) -gt 0 ]; then if [ $1 == "iceweasel" ]; then wmctrl ...
5
votes
3answers
317 views

Use bash's read builtin without a while loop

I'm used to bash's builtin read function in while loops, e.g.: echo "0 1 1 1 1 2 2 3" |\ while read A B; do echo $A + $B | bc; done I've been working on some make project, and ...
1
vote
2answers
366 views

Script to su -c into a screen is not working as intended

I'm working on a script that will allow me to load up a screen from a user via sudo and su -c. However, when I use it, it will just show the shell of the user: sudo su -c "script /dev/null; screen -r ...
1
vote
0answers
22 views

Questions about whiptail and bash functions

I'm a bash newbie. Say I have this script #!/bin/bash function cpp-lang { yum install "Development Tools" } function updatesys { yum -y update yum -y upgrade } whiptail ...
2
votes
2answers
2k views

Can I run Bash scripts in FreeBSD without modifying them?

Correct me if I'm wrong: "sh" script != "bash" script Linux script are written in Bash Bash script usually #!/bin/sh In GNU/Linux, /bin/sh is Bash In FreeBSD, /bin/sh is not bash, it's the true sh ...
0
votes
1answer
17 views

Setting up a script on an AMI box to create directories - vsftpd, pam, mysql

I'd like to first say I'm a noob when it comes to linux, so please excuse my confusion. I'm trying to setup vsftpd on a amazon ami 64 bit box, free tier micro. Following these instructions, ...
4
votes
1answer
104 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
2answers
242 views

How do I use a function return as a condition of an if else then shortcut?

There are instances where a function needs to execute and return to the caller and this works for me. if tst; then echo "success" else echo "failure" fi function tst () { return 0; } ...
1
vote
2answers
50 views

Copy the first 2 lines from a file and then remove the lines

I need to process a file using a shell script. The file arrives with the data as well as some embedded information releavant to how it is processed. The file is in the following format; ...
1
vote
1answer
36 views

split line based on space and delete the second part

I have a big file >fid|29290408|locus|VBIEntCas2262_0001| Phosphoglycolate phosphatase (EC 3.1.3.18) [Enterococcus casseliflavus EC20] ...
0
votes
1answer
25 views

Error while running script with nohup

I got this error while running my script with nohup sh: syntax error near unexpected token `(' My script is as follow: #!/bin/bash report_log="report.log"; grep -A 3 'successful\|ERROR COUNT ...
-1
votes
1answer
24 views

multiple input in a awk file

Please guide me to get out of it. Thanks in advance. I am having a situation that I have to write a awk script which takes the input of two .out files and generate a single .txt file. $ cat ...
3
votes
6answers
4k views

How to reverse a string made of digit in bash?

What is the best way to turn out digits in number? E.g. $ echo 123 | hook 321 $ echo 12358 | hook 85321
0
votes
1answer
515 views

Starting Resque worker with script doesnt work, starting by hand does

I am trying to figure out why my process doesn't work if started via a script, but works fine if started by hand. Here's the details: script is a resque worker start script. The script location is ...
2
votes
1answer
42 views

How do I securely download files in shell scripting?

I am writing some shell scripts to automatically download software packages off my server and set them up on customer servers. At some point within the script, the download command is wget ...
3
votes
3answers
70 views

The output I get via “echo” works as a command when pasted to terminal, but not in the script

I'm trying to change the files generated by an app that unfortunately often contain spaces. I have managed to get "echo" to give me output that works if copy pasted into terminal, but when I try to do ...
1
vote
1answer
74 views

Why does the printf statement in this loop output the array out of sequence?

When I execute the following code, the output of the printf statement appears to show array out of sequence. In the declare statement the Source item is declared before Destination and in the output ...
-5
votes
2answers
34 views

placing things in a users home directory with .deb

i am making a .deb installer and would like to place some files in the users home directory but i can not hard code a username. does any one know of any other places in the file system that i could ...
0
votes
1answer
25 views

How to automat password entry without user interference?

How to type password automatically without user interference. For example echo 'MyPassword@2014' > su UserName I want to automat this in my script. The script should not wait for input. It ...
0
votes
1answer
36 views

finding and replacing text [duplicate]

Does any one know how to find and replace text in a file. it can be implimented into a bash file. i would like to find the line and the output the text to it the file can only be opened with the root ...
0
votes
1answer
18 views

Bash script: array elements containing space character [duplicate]

I'm getting started with bash scripting. I'm working with array elements that contain space characters. In the code below, the third array element is "Accessory Engine". I tried to set the space ...
2
votes
1answer
204 views
+50

Unreadable backup directory on a USB drive

The title pretty much sums it up. I was editing a script I wrote some time ago to automatically backup the contents of my Raspberry Pi SD card, and in the course of editing, I missed a "/", and so ...
-4
votes
2answers
36 views

how to find a file extension with multiple dots [duplicate]

I'd like to extract the extension out of a filename with multiple dots. For instance: gbamidi-v1.0.tar.gz I should get "tar.gz", not "gz" or "0.tar.gz". I prefer a solution not relying on bash ...
0
votes
1answer
20 views

How to assign the cat output of a bash script to a variable in another script

I have a bash script that produces a cat output when it takes an argument. I also have another bash script that executes the first bash script with an an argument that I want to produce cat outputs ...
11
votes
3answers
2k views

Can't indent heredoc to match nesting's indent

If there's a "First World Problems" for scripting, this would be it. I have the following code in a script I'm updating: if [ $diffLines -eq 1 ]; then dateLastChanged=$(stat --format '%y' ...
1
vote
5answers
328 views

How to use grep and cut in script to obtain website URLs from an HTML file

I am trying to use grep and cut to extract URLs from an HTML file. The links look like: <a href="http://examplewebsite.com/"> Other websites have .net, .gov, but I assume I could make the cut ...
1
vote
1answer
402 views

Can I get the backup option of cp and mv to work with directories?

--backup[=CONTROL] make a backup of each existing destination file The man page does say that it works on file. It doesn't seem to have any effect on directories. Is there anyway to make this ...
1
vote
1answer
30 views

Save and restore terminal tabs with custom tabs title

I have a Terminal window with a dozen named tabs open *(gnome-terminal) I would like to save the current configuration and have it restored with names and directories. Is there a way to do this? I've ...
1
vote
2answers
150 views

Search for a string in the output of a command

I'm stuck with a simple script. I want to run tracepath on multiple address and to find if some IP is contained in the output of the tracepath. For now I have the following: if tracepath -n ...
1
vote
1answer
41 views

Rename files based on directory names

How can I rename .jpg files in multiple folders based on the name of the folder? I also want the .jpg files to remain in their respective folders and not moved. The folder names have no spaces. So, ...
0
votes
2answers
22 views

The best way to add Unix Shell variables to CURL GET request

Let’s say I use curl –X GET command with the following link: “$WEBSITE_URL/ACTION_NAME/ACTION1;id=$ID?Command=TEST&id1=$ID1&id2=$ID2&id3=$ID3” As you can see, there are some $variables ...
33
votes
3answers
60k views

What characters do I need to escape when using sed in a sh script?

Take the following script: #!/bin/sh sed 's/(127\.0\.1\.1)\s/\1/' [some file] If I try to run this in sh (dash here), it'll fail because of the parentheses, which need to be escaped. But I don't ...
2
votes
1answer
17 views

Is there an alternative to using rbash to restrict a user on my system?

Basically at the moment I am using rbash to stop a user from leaving his directory on my system. The problem with this is, there is a script in my bin which he needs to be able to run from a script in ...
-2
votes
1answer
39 views

How can I run a script as sudo and for it to not ask me for a password?

I want to run the following script: #!/bin/bash if grep -qs 'MinecraftServer' /media/kiancross/MinecraftServer/MinecraftServer; then echo "Disk already mounted, server boot will continue" else ...
1
vote
1answer
34 views

How can I pass the success (or failiure) of a command to a variable?

I am running rsnapshot to do backups of my NAS. I am forwarding the output of the command (e.g. rsnapshot hourly, rsnapshot daily etc.) to /tmp/rsnapshot_stats_hourly. In another script, I'd like to ...
0
votes
0answers
42 views

Increment Numbers in URL?

I want to create a list of URLs. The URLs have two sets of identical numbers which need to be incremented by 1: http://sample.com/poilus/cwrgshs/192/192.html ...
0
votes
1answer
9 views

How can I pass a function name to a test runner?

I wanted to write my own test runner: do_test () { function_under_test=$1 expected="$2" function_under_test $3 # <-- Line 32 if [ $result -eq $expected ]; then printf '.' else ...
1
vote
3answers
28 views

Loop to check whether a Debian package is installed or not

I like to check whether several packages are installed or not on Debian/Ubuntu Here is my attempt at a script to do this: query=`dpkg-query -W -f '${Status}'` ok="install ok" if ! [ `$query` curl ...
2
votes
3answers
984 views

Query list of servers for memory/cpu allocated

Running RHEL 6.2, trying to write a bash script to SSH to a list of remote servers, and write the CPUs and Total Memory to a file, one line per host in the following format: HOSTNAME1 CPUS: 12 ...
0
votes
1answer
28 views

Sourcing a redistributable script by default

I am creating a bash script which I wish to release on github. The script needs to access the command history but this is not possible without sourcing that script. I don't want users to put it in ...
0
votes
2answers
31 views

Why do I get Illegal number: {1..3}

Why am I getting ./6_sum_square_difference.sh: 11: ./6_sum_square_difference.sh: Illegal number: {1..3} for #!/bin/sh summing () { the_total=0 num_in=$1 for one_num in {1..$num_in} do ...
3
votes
2answers
47 views

Why alias inside function does not work?

See the code below: a()(alias x=echo\ hi;type x;alias;x);a I have an alias inside a function, I do not want to change the external environment (that is why I am using () instead of {}), even the ...
0
votes
1answer
45 views

Why does this comparison return not equal?

Can't seem to get this to evaluate to true is_equal () { in="$1" if [[ "$in" == "385" ]]; then return 0 else return 1 fi } a= is_equal 385 if [[ "$a" ]]; then echo "equal" else ...
1
vote
1answer
65 views

Searching file based on data range

I previously posted requesting help with counting occurrences of a string. I'm now hoping to search for the occurrence of a string within a range of values and print out a similarly formatted file ...
2
votes
2answers
61 views

How to return 0 if a pattern is matched from a file? [duplicate]

I have a binary that repacks android kernel (not mkbootimg). I'm making a script to automate the process. I don't want any output on the screen. So I have redirected the output to a file named ...
-1
votes
1answer
246 views

shell script - Unable to execute Java with parameters

I have written a shell script that will take a parameter, pass that parameter to Java command. But I get error during the execution. Here is the script: #The script takes an input, the file name to ...
2
votes
3answers
55 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 ...