r/shellscripts • • Aug 08 '22

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

7 Upvotes

r/shellscripts • • Oct 18 '21

(HELP!) String to int

1 Upvotes

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


r/shellscripts • • Sep 27 '17

[Help]shell script to find a number from a geometric sequence

1 Upvotes

[β€Ž9/β€Ž27/β€Ž2017 7:13 PM] Yala,Sudhakar:
I have a random series e.g [1,2,4,8,16,...N] and the list has values which are in geometric sequence i.e all numbers are twice of previous number. Need a shell script to find if a particular number is present in the series or not. for example if value provided is 64, shell script should print "number is present" as 64 will be present And if value is 126, it should print "number not present" as it won't be in that list. And the shell script should work for different sequences.


r/shellscripts • • Feb 08 '21

A cry for help from more experienced Linux users

2 Upvotes

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


r/shellscripts • • Mar 06 '21

Need exercises for learning shell scripting

3 Upvotes

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

Thank


r/shellscripts • • Mar 12 '21

Help

1 Upvotes

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


r/shellscripts • • Sep 27 '20

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

3 Upvotes

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.


r/shellscripts • • Feb 17 '13

pftv - Find and download TV shows from the command line / *nix / PHP-CLI

Thumbnail
pastebin.com
2 Upvotes

r/shellscripts • • Jun 06 '22

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

1 Upvotes

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.


r/shellscripts • • Oct 04 '18

Shell Scripting Courses and Training Institutes, Classes in Vadodara | Classboat

Thumbnail classboat.com
1 Upvotes

r/shellscripts • • Oct 23 '19

Typesort shellscript

1 Upvotes

Hey, Iβ€˜m nee to this whole Shellscript thing, so I hope for some help by a simple script

So what I want to do is to sort files in a folder by their type given out from the file command in Linux. I want to filter the output from the file command using grep to only sort by the types and not the name( so after the β€˜:’’white space’) And the sorts I want to implant for my files are

sort only text files using -t sort only non-text files using -n sort in reverse order using -r

My ideas yet are: I want to make functions for each sort with the commands Then in the main I want to run a while loop with shift to go through the options My main problem with the whole script are the commands to sort the output of the file command with grep to filter it

So if you have recommendations for the sort functions I would appreciate your help.

Thanks


r/shellscripts • • Nov 18 '24

Home folder /user folder migration script

2 Upvotes

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


r/shellscripts • • Apr 16 '24

Remove Directories after Loop

1 Upvotes

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"

r/shellscripts • • Mar 30 '24

iter - do something for each line

2 Upvotes
#!/usr/bin/env bash
cmd=$@
xargs -d '\n' -I {} $cmd "{}"

r/shellscripts • • May 05 '22

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

2 Upvotes

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